Good day. I am learning Java and this question has appeared:

What is the x variable after the next line?

int x = (-14) | 7; 

The fact is that here the correct answer was -9.

I reasoned like this, in the binary number system 14 is represented as 1110. But since the number 14 with the sign "-" then it turns out that you need to use additional code. It turns out that for the number 14 the additional code is equal to:

 1110 ->0001(инвертируем все разряды)->0010(добавляем +1)->10010(Дописываем слева знаковый единичный разряд) 

Now we perform the logical OR operation on two binary representations of numbers:

 10010 + 00111 

and it turns out 10111 that in translation into an integer equals to 23. Explain, please, what am I doing wrong? It seems everything is by the rules, but the result is still not correct. Thank you in advance.

  • Make the transition from signed numbers to numbers without it. 10111 is the sign (1) and 0111 -> (in the additional code) 1001 = 9. So everything is fine. And 23 with four digits does not happen - alexlz
  • That is, I reasoned correctly. Just when I got the number 10111, then 1 at the beginning is its sign, and then comes the very representation of the number in the additional code, which needs to be translated into a direct representation in the reverse order of how to do the reverse code. So it turns out? - Drylozav
  • Well, if you submitted so -114, then yes. As you have already been explained, the sign bit is much to the left, but all 32-4 = 28 bits in your case are ones. You can paint them all, but nothing will change. Simply it would be more difficult for you to forget about the significant discharge. - alexlz
  • That is, you need to record all 32 digits anyway? That is, if the example changes, then my reasoning will no longer be correct? - Drylozav

2 answers 2

you have an error, you incorrectly submitted a number .. (not a sign added there) (here I think that int == 4 bytes (in java this is true, but C \ C ++ is not a fact))

 1111 1111 1111 1111 1111 1111 1111 0010 (-14) 0000 0000 0000 0000 0000 0000 0000 0111 (7) 

perform OR

 1111 1111 1111 1111 1111 1111 1111 0111 (-9) 

UPD (response to the comment: "But how did you determine that the number 10111 is represented as -9")

 1111 1111 1111 1111 1111 1111 1111 0111//отрицательное число (-9) 1111 1111 1111 1111 1111 1111 1111 0110//вычли 1 0000 0000 0000 0000 0000 0000 0000 1001//инвертировали 

or the same

 1111 1111 1111 1111 1111 1111 1111 0111//отрицательное число (-9) 0000 0000 0000 0000 0000 0000 0000 1000//инвертировали 0000 0000 0000 0000 0000 0000 0000 1001//добавили 1 

UPD 2

article on java and bitwise operations

  • I also got the number 10111 (you have the last 5 characters). But how did you determine that the number 10111 is represented as -9? - Drylozav
  • one
    @Drylozav so additional code. Invert, add one, get 9 - alexlz
  • Tell me, did you write "not there the sign was added" Can you explain what you had in mind? - Drylozav
  • Thank you very much . Available explained. Only you can tell where you can read about it in more detail? - Drylozav
  • I did not put it that way ... I immediately presented the number in bytes in my head (1,2,3 or 4 does not matter) and therefore I said that they put it in the wrong place, in the comments above you were told that it was not necessary to take the first digit into account (which would the same result) - ProkletyiPirat

according to java rules, in such operations numbers are equated to int. And this is 4 bytes (32 bits). And in your reasoning this is not visible.

 -14 => 11..110001 7 => 00..000111 

the result of a logical or

  => 11..110111 

Since the most significant byte is 1, this is a negative number. Therefore, we decode through an additional representation.

 11..110111 => -00...001001 => -9 
  • according to java rules, in such operations numbers are equated to int. Tell me, where can I read this in more detail? - Drylozav 1:51 pm