aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help counting negative numbers in binary! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help counting negative numbers in binary!" Watch "Help counting negative numbers in binary!" New topic
Author

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!
Thanks for your help,
Mark
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Good job Mark
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Don't get me started about those stupid light bulbs.
 
subject: Help counting negative numbers in binary!
 
Similar Threads
Shift !! ??? :confused:
Binary calculations
Shift !! ??? :confused:
Operators and Assignments ...
confused by shift operators