Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Help counting negative numbers in binary!

Mark Fletcher
Ranch Hand
Posts: 897
Hi,
This sounds like I should go back to kiddies school for posting something so rudimentary as this, but one of the big problems Ive had in sitting pass papers is the shift operators (>>, <<, >>>) and negative numbers. Ive got the exam coming up this Wednesday.
You see I can count positive numbers in binary ie
0001 = 1
0010 = 2
0011 = 3 and so on and so forth
But negative numbers..... I cant!
I can understand how the shift operators work, but unfortunately not how to count negative numbers in binary
Any help would be greatly appreciated!
Cheers,
Mark

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
to get the negative counterpart of a positive number you have to do the following:
00101101 = 45
There are two basic steps to perform:
1. change all 0's to 1's and 1's to 0's
11010010
2. add 1 to the result
11010011
this last bit sequence is -45 (you notice the 1 on the left which indicates that it is a negative number)
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

Mark Fletcher
Ranch Hand
Posts: 897
Thanks Valentin!
Thats allowed me to answer a lot of questions that I might otherwise have got wrong... Ive just gone over all the pass papers with those kind of questions and Ive got them correct!