Could someone explain to me why the mask is not shifted to the right at all? You can use anything in place of that 1 and the result will be the same.
unsigned mask = ~0 >> 1;
printf("%u\n", mask);
The right shift operator ( >> ) returns the signed number represented by the result of performing a sign-extending shift of the binary representation of the first operand (evaluated as a two's complement bit string) to the right by the number of bits, modulo 32, specified in the second operand.
The bitwise shift operators move the bit values of a binary object. The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted. The result is not an lvalue.
The Right Shift Operator moves the bits of a number in a given number of places to the right. The >> sign represents the right shift operator, which is understood as double greater than. When you type x>>n, you tell the computer to move the bits x to the right n places.
>> is the signed right shift operator. It shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. When you shift right two bits, you drop the two least significant bits.
It's a type issue. If you cast the 0 to unsigned it'll be fine:
unsigned mask = ~ (unsigned) 0 >> 1;
printf("%u\n", mask);
Edit per comments: or use unsigned literal notation, which is much more succinct. :)
unsigned mask = ~0u >> 1;
printf("%u\n", mask);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With