• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

bit representation of negative values

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot everyone
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic