Why does printf ("% d", ~ 0) print -1?

Why printf("%d", ~0);does it matter -1? Shouldn't it be 1, since the operator ~converts every 1-bit to 0-bit and vice versa?

From what I understand 0000will be reset to 1111.

+4
source share
5 answers

~- bitwise addition operator. 00000000flips up to 11111111.

If you take the 2'sadd-on 1(00000001), you will get 11111111one that should represent -1in binary format.

Therefore, the conclusion -1.

+2
source

Try to see its hexadecimal part.

printf("%x", ~0);

ffff, , , 0000000000000000 1111111111111111. , 1, . , -1.

+2

C/++, ~ - . - x , x. , x 0b00000000, 0x11111111.

!!!!!

-1. , , .

x, -x x.

x, -x x.

() , ! , , -0, .

BTW, , ( ) , (unsigned , , ).

+2
source

On a 32-bit machine, they 0appear as

00000000000000000000000000000000

As with biwise NOT (~), then it flips all the bits, and then it represents a bit representation, for example, be

11111111111111111111111111111111 

which is a -1representation of 2s.

+2
source
%d takes signed integer

different variations of same input

 Signed Value ~0 -1
UnSigned Value ~0 4294967295
Hex  Value ~0 ffffffff
Hex  Value ~0 FFFFFFFF

as you can see that the signed bit is set and its nothing but 2 compliments 1

usually

-ve of n = (~n) + 1;
      complement of 5  is  -6 
 and  complement(5)+1  is   -5
+1
source

Source: https://habr.com/ru/post/1608181/


All Articles