File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Help counting negative numbers in binary!

Mark Fletcher
Ranch Hand

Joined: Dec 08, 2001
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

Mark Fletcher - http://www.markfletcher.org/blog
I had some Java certs, but they're too old now...
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
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

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mark Fletcher
Ranch Hand

Joined: Dec 08, 2001
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!
Mark
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Good job Mark
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

I agree. Here's the link: http://aspose.com/file-tools

subject: Help counting negative numbers in binary!