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
posted
0
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
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
posted
0
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
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