• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Bitwise inversion operator question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test{
public static void main(String args[]){
byte x=3;
x=(byte)~x;
System.out.println(x);
}
}
A. 3
B. 0
C. 1
D. 11
E. 252
F. 214
G. 124
H. -4
The correct answer was given as H. The answer i got was -124. Could someone please explain this question to me.
 
Enthuware Software Support
Posts: 4904
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure how are you getting -124. Did you type it exactly as you've given here?
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
~n = -n - 1
Can anyone confirm this?
 
Paul Anilprem
Enthuware Software Support
Posts: 4904
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct. ( but only if there is no overflow etc.) For eg. It won't work in cases like this:
int x = 128;
byte b = (byte) ~x; //here b will be 127! Now figure it out.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test{
public static void main(String args[]){
byte x=3;
x=(byte)~x;
System.out.println(x);
}
}
the ~ operator simply works like this.
~X is the same as (-X)-1: //just change the sign and deduct 1.
//in this case
~x = (-3)-1
which is the same as -4.
 
sola yemi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info about the overflow, if i'd seen this in the exam, i would have been totally confused, thanks again.

Originally posted by Paul Anil:
That's correct. ( but only if there is no overflow etc.) For eg. It won't work in cases like this:
int x = 128;
byte b = (byte) ~x; //here b will be 127! Now figure it out.


 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 in binary(byte) is 00000011
if you inverse it, it should become 11111100 which is equal to 252 in decimal.
Is that not correct???
Please explain...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 in binary(byte) is 00000011
if you inverse it, it should become 11111100 which is equal to 252 in decimal.
Is that not correct???
Please explain...
------------
Dev, Signed bit and 2's complement , that is the catch.
 
srival vas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------
Originally posted by Paul Anil:
That's correct. ( but only if there is no overflow etc.) For eg. It won't work in cases like this:
int x = 128;
byte b = (byte) ~x; //here b will be 127! Now figure it out.
-----------------------------------------------------------------
just to clarify though I am sure you all realised it,
b is 127 not bcos of ~, but bcos -129 (-128 -1 ) has
been casted to byte, I would assume I can still safely
assume ~n = -n -1 and then concern myself with the casts et al.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jesse,
The answer for your question is -4. how?
here is explanation....
public class test{
public static void main(String args[]){
byte x=3;
x=(byte)~x;
System.out.println(x);
}
}
A. 3
B. 0
C. 1
D. 11
E. 252
F. 214
G. 124
H. -4

byte x=3 is in 8 bit representation so, in binary it is 00000011 OK! then
~x= bit wise inversion is 11111100 OK! after this inversion
when right most bit is 1 it will be considered as a
negative value and it is assigned to x.In JVM the negative numbers will be represented in 2's complement form ,so to get actual number you first again inverse it and add 1 to last significant bit and express your result in nagative value, so this is like this
x: 00000011
~x: 11111100

for result inverse the above ~(~x): 00000011
add 1 to left most bit 1
--------
00000100 which is 4 in
--------
decimal notation and express result in negative i.e.:-4
OK! NOW I THINK YOUR DOUBT IS CLEARED.
---THANKS
SHIVARAM
 
psr krishna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jesse,
please correct a little mistake in my previous post

byte x=3 is in 8 bit representation so, in binary it is 00000011 OK! then
~x= bit wise inversion is 11111100 OK! after this inversion
when left most bit is 1 it will be considered as a
negative value and it is assigned to x.In JVM the negative numbers will be represented in 2's complement form ,so to get actual number you first again inverse it and add 1 to right significant bit and express your result in nagative value, so this is like this
x: 00000011
~x: 11111100

for result inverse the above ~(~x): 00000011
add 1 to right most bit 1
--------
00000100 which is 4 in
--------
decimal notation and express result in negative i.e.:-4
OK! NOW I THINK YOUR DOUBT IS CLEARED.
---THANKS
SHIVARAM
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is -4 only.
Try it with the jdk and see the result yourself.
3 decimal = 0000 0011 ( binary )
when you invert it becomes 1111 1100 ( binary )
That's 2complement representation of -4 ( check by 1. inverting and 2. adding 1 to result of inversion ).
You should check once more.
i hope it helps.
raghav..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic