aspose file tools*
The moose likes Beginning Java and the fly likes Negative numbers shown in bits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Negative numbers shown in bits" Watch "Negative numbers shown in bits" New topic
Author

Negative numbers shown in bits

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Okay.. so here's an easy one for you. How can I represent -4 in bits. Any good rule of thumb would be great so its easy to remember how to do it later. I understand the positive 4. Thats easy. 0100
Thanks
-Dale
------------------
What's this H2SO4 doing in my fridge?? ( thud )


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Read this:
http://www.javaranch.com/ubb/Forum33/HTML/001584.html


"JavaRanch, where the deer and the Certified play" - David O'Meara
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Dale
To find the negative value of a number you use the twos compliment:
1. take the positive number and flip all the bits (turn 1's into 0's and 0's into 1)
2. add 1 to the result
so for your example of -4
4 is
00000000 00000000 00000000 00000100
flip the bits
1111111 11111111 11111111 11111011
add 1
1111111 11111111 11111111 11111100
this is -4 in binary.

hope that helps
Dave

Dave
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Thanks
Dale

------------------
What's this H2SO4 doing in my fridge?? ( thud )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Negative numbers shown in bits