Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Programmer Certification (OCPJP)
Bitwise complement operator
d jones
Ranch Hand
Posts: 76
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
In K & B book for
SCJP
1.4, on page 175, the bitwise complement (~) operator is explained:
int x = 5; //line 1 x = ~x; //line 2
After line 2 the value of x is -6.
In bit representation 5 is:
0000 0000 0000 0000 0000 0000 0000 0101
the flip-the-bits operator converts it to:
1111 1111 1111 1111 1111 1111 1111 1010
How do I convert the converted bit value back to -6?
Keith Lynn
Ranch Hand
Posts: 2412
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
~x = -x - 1
d jones
Ranch Hand
Posts: 76
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Keith,
Is that a rule which I can apply unconditionally all of the time?
Keith Lynn
Ranch Hand
Posts: 2412
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yes, in all cases ~x = -x - 1.
Tom Riddle
Greenhorn
Posts: 8
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
int x = 5
bit
pattern
is 00000000 00000000 00000000 00000101
~x pattern is 11111111 11111111 11111111 11111010
Now since the MSB(most significant bit) is 1 we know its a -ve number. To get the actual value of the -ve number take 2's complement of the number
00000000 00000000 00000000 00000101
+ 1
-----------------------------------
00000000 00000000 00000000 00000110
which is nothing but 6
so ~x = -6
Regards,
Chaitra
Bert Bates
author
Posts: 9050
21
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
and of course this is only a 1.4 topic, not 5.0
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Negative Binary representation
bitwise complement operator
Question on Shift Operators
~30
Question on Bitwise Complement Operator
More...