JavaRush /Java Blog /Random EN /Bitwise negation - why is that so?
Alex Bolgov
Level 37
Ташкент

Bitwise negation - why is that so?

Published in the Random EN group
Hello everyone :) While reading the article Bitwise operations, I, like some others, had a question: why, when we invert the number 342, we get -343. This is how it looks in the article: Bitwise negation - why so - 1Those who are especially inquisitive (like me) have a question: “hey, but 010101001 is less than 101010110, it is logical that it should be less than 342 and positive.” By typing it into any online number converter or checking it using the code:
Integer a = Integer.valueOf("010101001", 2);
System.out.println(Integer.toString(a, 10));
unexpectedly we get something other than -343...
169
Oh, how good it is that once upon a time, in a class on programming in C, I somehow briefly heard something about the sign digit in integers. After Googling for a while, I can now explain what kind of animal this is. Bitwise negation - why so - 2In fact, there is an inaccuracy in the article, which is confusing. If we check with the code what is really the result of inverting the number 342:
int a = 342;
System.out.println(Integer.toBinaryString(~a));
then we will see a slightly different result
11111111111111111111111010101001
Why is this so? The thing is that a variable cannot simply contain 101010110; in fact, it is stored as
000000000000000000000000101010110
After all, an int variable takes 4 bytes, i.e. 32 bits - 32 memory cells. After inversion, absolutely all numbers change, i.e. and "trimmed" leading zeros. Now comes the fun part. Since in the binary representation the + or - sign in front of a number cannot be stored, there is one trick: the very 1st bit is actually responsible for the sign and is precisely the sign bit . And all numbers are stored according to this logic: numbers from 00...000 to 01...111 are positive, starting from 0 (i.e. from 0 to 2147483647) and starting from 10...000 to 11... 111 are negative ones, starting from the smallest and ending with -1 (i.e. -2147483648 to -1).
00000000000000000000000000000000 = 0
01111111111111111111111111111111 = 2147483647
10000000000000000000000000000000 = -2147483648
11111111111111111111111111111111 = -1
And then you ask, “Okay, Navalny, we’ll believe you, but why did you decide that this is his palace,” but how does it turn out to be -343 with all this? It's really simple. We can simply calculate this manually. If we “cut off” (or replace with 0) the very first bit from the binary version of the resulting number 342 ( 1111111111111111111111111111111111111111010101001 ) and return it back to decimal form.
String s = "01111111111111111111111010101001";
System.out.println(s + " = " + Integer.toString(Integer.valueOf(s, 2), 10));
then we get that
01111111111111111111111010101001 = 2147483305
In fact, here we didn’t just “cut off” 1 at the beginning, but subtracted 1000000000000000000000000000000000 from this number. Now let’s add it back, but in decimal form. What we will see:
~342 =
11111111111111111111111010101001 =
//в двоичном виде//
01111111111111111111111010101001 +
10000000000000000000000000000000
=
//в десятичном виде//
-2147483648 + 2147483305 = -343
Here is the required number -343 :) Guys, this is my first post here, please like it if you liked it and it was informative (I really want to collect all the achievements 😄) You can read more about this topic here: Good luck to everyone and thank you for your attention)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION