Does ~0 mean its flipping 000000000 to 1111111111?
printf("Check: %i", ~0);
The printf results to -1, which is why I am confused.
Does -1 essentially mean the same thing as 11111111111111111 bits?
bitbitwise-operatorsc++
Does ~0 mean its flipping 000000000 to 1111111111?
printf("Check: %i", ~0);
The printf results to -1, which is why I am confused.
Does -1 essentially mean the same thing as 11111111111111111 bits?
Use:
~0U >> 1
Suffix 'U' for unsigned shift behavior.
so, confused that why not
~0
turns out to be0xffffffff
?
See, what is 0
say in four bytes representation:
BIT NUMBER 31 0
▼ ▼
number bits 0000 0000 0000 0000 0000 0000 0000 0000
▲ ▲
MSB LSB
LSB - Least Significant Bit (numbered 0)
MSB - Most Significant Bit (numbered 31)
Now ~
is bitwise not operator then flips all bits in 0
as:
BIT NUMBER 31 0
▼ ▼
number bits 1111 1111 1111 1111 1111 1111 1111 1111
▲ ▲
MSB LSB
Because of MSB = 1
this representation is treated as negative number and its magnitude is find using 2'complement math that is -1
.
What is 1
? it is:
number bits 0000 0000 0000 0000 0000 0000 0000 0001
▲ ▲
MSB LSB
1's complement of 1
number bits 1111 1111 1111 1111 1111 1111 1111 1110
▲ ▲
MSB LSB
2'complement? Add 1
in one's complement, that is:
number bits 1111 1111 1111 1111 1111 1111 1111 1111
▲ ▲
MSB LSB
this same as when you gets ~0
? that is why you are getting -1
output.
In most implementation of C >> operator is defined as an arithmetic right shift, which preserves the sign bit MSB. So ~0 >> 1
is noting but -1
remains same.
6.5.7 [Bitwise shift operators]
5 The result of
E1 >> E2
isE1
right-shiftedE2
bit positions. IfE1
has an unsigned type or ifE1
has a signed type and a nonnegative value, the value of the result is the integral part of the quotient ofE1 / 2E2
. IfE1
has a signed type and a negative value, the resulting value is implementation-defined.
You requirement is what is called unsigned right shift >>
and the behavior you needed can be find using unsigned number that is why I suffixed U
as 0U
.
Because printing INT_MIN and INT_MAX is bit tricky(because of undefined and implementation behavior of setting MSB and bit-overflow) in C so I have written a code as follows:
#include <stdio.h>
#include<limits.h> /* include for CHAR_BIT */
int main(){
int my_int_min = 1U << ((sizeof(int) * CHAR_BIT) - 1);
int my_int_max = ~0U >> 1;
printf("INT_MIN = %d\n", my_int_min);
printf("INT_MAX = %d\n", my_int_max);
return 0;
}
See it executing @codepad, it output is:
INT_MIN = -2147483648
INT_MAX = 2147483647
Note for 32-bit number range is [-2147483648, 2147483647]
that is equals to [-231, 231 -1 ]
.
INT_MIN: -231 == -2147483648 is:
1000 0000 0000 0000 0000 0000 0000 0000
▲ ▲
MSB LSB
In expression 1U << ((sizeof(int) * CHAR_BIT) - 1)
, I shifts first bit the LSB(that is 1) to left most side at MSB, And because in C, setting signed bit is undefined behavior when operand is singed type so I used unsigned one 1U.
6.5.7 [Bitwise shift operators]
The result of
E1 << E2
is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.
Another point to note is I used CHAR_BIT a standard macro defined in limits.h that tells number of bits in one char in a C implementation (remember: A char is always one byte size but number of bits in one bytes can be different on different system not always guaranteed to be 8).
INT_MAX: 231 -1 == 2147483647
0111 1111 1111 1111 1111 1111 1111 1111
▲ ▲
MSB LSB
5.3.1/10 The operand of
~
shall have integral or unscoped enumeration type; the result is the one’s complement of its operand. Integral promotions are performed. [emphasis mine]4.5/6 A prvalue of type
bool
can be converted to a prvalue of typeint
, withfalse
becoming zero andtrue
becoming one.4.5/7 These conversions are called integral promotions.
So ~false
is an int
with a bit pattern consisting of all ones - one's complement of a bit pattern representing 0, namely all zeros (as required by 3.9.1/7.) Similarly, ~true
is an int
that's one's complement of the bit representation of 1 - namely, all ones with the least significant bit zero. Both these values will evaluate to true
in boolean context.
Best Answer
Yes, it does.
In 2s complement representation, it does.