1

I was seeing how to represent binary numbers in java. one option is to use as String and use Integer.parseInt() to get the decimal value;

The other option (assignment of 2 to b):

int b = 0b0010; //2
System.out.println(b);
System.out.println(Integer.toBinaryString(-2));

output:

2
11111111111111111111111111111110

using this format, how to represent -2:

int c=??//-b
3
  • 2
    Are you familiar with two's complement arithmetic? Commented Dec 26, 2013 at 23:26
  • @vandale: I am very familiar with two's complement. I am interested in the representation, not on how to calculate negative binary numbers Commented Dec 26, 2013 at 23:31
  • 2's complement relies on a fixed word size, so it would have to be implied then that the value is, say 32 bit long Commented Dec 26, 2013 at 23:33

3 Answers 3

5

int values are stored in 32 bits where the most significant bit is the sign.

0b0010; //2

is actually

0b00000000_00000000_00000000_00000010; //2

To convert that to a negative number, you flip the 0s to 1s and the 1s to 0s` and add 1. So

0b00000000_00000000_00000000_00000010; //2
0b11111111_11111111_11111111_11111101
0b11111111_11111111_11111111_11111110; // +1

And so

int b = 0b11111111_11111111_11111111_11111110;

would have the value -2.

Sign up to request clarification or add additional context in comments.

5 Comments

oh, to represent negative numbers, I need to type in all 32 bits, whereas for positive numbers such as 2, 0b0010 is sufficient..
@eagertoLearn Right, since its the first bit that decides the sign, you need to put all the bits between that and the value you are trying to represent.
@SotiriosDelimanolis Sorry but i don't think your right. check my answer!
@rullof We're talking about two different things. If you want to use an integer literal to represent -2 in binary, you need to specify all the bits. An alternative way to get -2 is to use the ~ operator as you are suggesting. Another alternative is to just write -2. Another alternative is to write 0 - 2. Ad nauseam.
@SotiriosDelimanolis Ok that's right it depends on the point of view
2

Use the Bitwise NOT operator:

00000000000000000000000000000010 // 2 = 0b0010
00000000000000000000000000000001 // 1 = 0b0010-0b0001
11111111111111111111111111111110 // ~1 = ~(0b0010-0b0001)

11111111111111111111111111111110 // -2 = ~(0b0010-0b0001)

So you just subtract 0b001 and flip all the bits with the ~ Bitwise operator:

int b = ~(0b0010-0b0001); // ~(2-1) = ~1 = -2
System.out.println(b);
System.out.println(Integer.toBinaryString(b));

Demo

Comments

1

You are confusing storage with presentation. int b has no base, regardless of whether you assign 2, 0b010 or 0x02 to it. Base 10 is just something println assumes.

You can use Integer.toString(number, radix) to print properly signed binary numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.