# Binary numbers

Arathi Rajashekar

Ranch Hand

Posts: 112

posted 14 years ago

- 0

Hi all,

I have doubt. Can anybody explain me how to take 2's comlement of a -ve number.

What I know is we have to invert all 0's to 1 and 1 to 0. and add 1. The result will represent the 2's complement of -ve number

But I tried that way

Example -5

5 => 00000101

by inverting 0 to 1 and 1 to 0's

11111010

adding 1

-5 => 11111011

This value is correct when I checked it out using toBinaryString() of Integer class

But when I tried with -12

I did the same way as above

12 => 00001100

inverting we get 11110011

by adding we get

-12=> 11110011

Whereas when i found out the value for -12 with toBinaryString(), the answer was 11110100

The same thing is happening for other numbers also. For some number 2' complement procedure is working for other number it is not. So please tell me how to solve this.

I have doubt. Can anybody explain me how to take 2's comlement of a -ve number.

What I know is we have to invert all 0's to 1 and 1 to 0. and add 1. The result will represent the 2's complement of -ve number

But I tried that way

Example -5

5 => 00000101

by inverting 0 to 1 and 1 to 0's

11111010

adding 1

-5 => 11111011

This value is correct when I checked it out using toBinaryString() of Integer class

But when I tried with -12

I did the same way as above

12 => 00001100

inverting we get 11110011

by adding we get

-12=> 11110011

Whereas when i found out the value for -12 with toBinaryString(), the answer was 11110100

The same thing is happening for other numbers also. For some number 2' complement procedure is working for other number it is not. So please tell me how to solve this.

Arathi<br />Sun Certified Java Programmer

Shivaji Marathe

Ranch Hand

Posts: 203

posted 14 years ago

- 0

Arathi

Here is part of your post

you have inverted 12 but not added 1 to it.

Inverting 12 , we get 11110011 ,

when you add 1 to it

you get 11110100

which is -12.

Hope this helps

Here is part of your post

I did the same way as above

12 => 00001100

inverting we get 11110011

by adding we get

-12=> 11110011

you have inverted 12 but not added 1 to it.

Inverting 12 , we get 11110011 ,

when you add 1 to it

you get 11110100

which is -12.

Hope this helps

Arathi Rajashekar

Ranch Hand

Posts: 112

posted 14 years ago

Thanks shivaji. I got it. Will you tell me one more thing when you are adding one which kind of binary addition you are doing

- 0

Originally posted by Shivaji Marathe:

Arathi

Here is part of your post

you have inverted 12 but not added 1 to it.

Inverting 12 , we get 11110011 ,

when you add 1 to it

you get 11110100

which is -12.

Hope this helps

Thanks shivaji. I got it. Will you tell me one more thing when you are adding one which kind of binary addition you are doing

Arathi<br />Sun Certified Java Programmer

Dave Vick

Ranch Hand

Posts: 3244

posted 14 years ago

- 0

Arathi

It looks like your problem is in the adding of 1 to the number. When you add a number in base 10 we all know you have to 'carry' anything over 10 to the next number, the same ting applies to binary numbers too (as well as hex, octal and all the others).

With your example of -12:

12 is:

00000000 00000000 00000000 00001100

the twos compliment is

11111111 11111111 11111111 11110011

when you add one to the least significant bit which is a 1 the result is 2 but you cant ahve a two so you have to make it a 0 and carry over the extra to the next bit. So you carry a 1 to the next bit which is also a 1. Again, you'll end up with a 0 as the result and carrying a 1 to the next bit which is a 0. Adding the 1 the result is 1 so you no longer have to carry any extra and the answer is:

11111111 11111111 11111111 11110100

Notice also that I used all 4 bytes to do this, many times it's ok to get by with just using one byte but sometimes it'll give you the wrong answer. As an example figure this out using only 8 bits then using all 32: -5 >>> 2

hope this helps

[ January 16, 2002: Message edited by: Dave Vick ]

It looks like your problem is in the adding of 1 to the number. When you add a number in base 10 we all know you have to 'carry' anything over 10 to the next number, the same ting applies to binary numbers too (as well as hex, octal and all the others).

With your example of -12:

12 is:

00000000 00000000 00000000 00001100

the twos compliment is

11111111 11111111 11111111 11110011

when you add one to the least significant bit which is a 1 the result is 2 but you cant ahve a two so you have to make it a 0 and carry over the extra to the next bit. So you carry a 1 to the next bit which is also a 1. Again, you'll end up with a 0 as the result and carrying a 1 to the next bit which is a 0. Adding the 1 the result is 1 so you no longer have to carry any extra and the answer is:

11111111 11111111 11111111 11110100

Notice also that I used all 4 bytes to do this, many times it's ok to get by with just using one byte but sometimes it'll give you the wrong answer. As an example figure this out using only 8 bits then using all 32: -5 >>> 2

hope this helps

[ January 16, 2002: Message edited by: Dave Vick ]

Dave

It is sorta covered in the JavaRanch Style Guide. |