IntelliJ Java IDE
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes bit representation of negative values Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "bit representation of negative values" Watch "bit representation of negative values" New topic
Author

bit representation of negative values

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
1. can someone tell me how to represent byte b = -3 in bits ?
2. also how to represent int i = -6 ?
please explain the technique of doing this
thanks
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Sarim,
A byte is made up of 8-bits which can represent 256 numbers, 0 to 255 or -127 to 127 using two's complement arithmetic.
<pre>
0 = 0000 0000
255 = 1111 1111
127 = 0111 1111 // 0 in first bit represents + sign
-127 = 1000 0000 // 1 in first-bit represents - sign
</pre>
Two find the bit representation of a negative number, subtract the number from the highest value the type can represent.
So, to find the bit representation of -3, 256-3 = 253. The bit representation of 253 is:
<pre>
255 = 1111 1111
- 2 = 0000 0010 // need to get to 253, so subtract 2
---------
253 = 1111 1101 // -3 bit representation
</pre>
To find -6: 256 - 6 = 250
<pre>
255 = 1111 1111
- 5 = 0000 0101 // need to get to 250, so subtract 5
---------
250 = 1111 1010 // -6 bit representation
</pre>
I have some notes posted on working with binary, octal and hexadecimal numbers at file:///D|/Java/jeg/oper/binhex.html.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
lakshmi nair
Ranch Hand

Joined: Oct 11, 2000
Posts: 63
Negative numbers are 2's complement of positive numbers, ie,
1's complement + 1.
if we have -3 , just complement the bit pattern of 3 and add 1.
0000 0011 -1's complement---> 1111 1100 + 1 = 1111 1101
HTH
Lakshmi
Rob Whelan
Ranch Hand

Joined: Oct 18, 2000
Posts: 33
Originally posted by Jane Griscti:
<snip>
I have some notes posted on working with binary, octal and hexadecimal numbers at file:///D|/Java/jeg/oper/binhex.html.

Hi, Jane -- great explanation of two's complement, but your link won't help much (points to your hard drive instead of an internet URL...)
-Rob
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
thanks a lot everyone
Lisa Yanchunis
Greenhorn

Joined: Nov 03, 2000
Posts: 27
I found the following helpful for 2's complement
http://barada.canisius.edu/~meyer/253/BOOK/ch6/FULLPAGE/ch6-4.html
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Oops ... that was dumb ... here's the correct link
http://webhome.idirect.com/~jgriscti/oper/binhex.html
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker
 
 
subject: bit representation of negative values
 
Threads others viewed
Nested Classes in UML
Invalid character(Unicode: 0x7)
how is 1 second represent as a 'long'
FFMS & TMS
Relationship between classes
MyEclipse, The Clear Choice