File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bitwise inversion operator question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bitwise inversion operator question" Watch "Bitwise inversion operator question" New topic
Author

Bitwise inversion operator question

jesse
Greenhorn

Joined: Oct 10, 2000
Posts: 3
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.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
~n = -n - 1
Can anyone confirm this?
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
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
sola yemi
Greenhorn

Joined: Sep 14, 2000
Posts: 9
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

Joined: Sep 14, 2000
Posts: 9
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.

Dev Prakash
Greenhorn

Joined: Oct 09, 2000
Posts: 28
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...
srival vas
Greenhorn

Joined: Oct 10, 2000
Posts: 10
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

Joined: Oct 10, 2000
Posts: 10
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.
psr krishna
Greenhorn

Joined: Oct 05, 2000
Posts: 12
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

Joined: Oct 05, 2000
Posts: 12
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
Raghvendra Sharma
Ranch Hand

Joined: Oct 09, 2000
Posts: 82
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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bitwise inversion operator question