I'm wondering what the correct right-hand operand is for C/C++ bit-shift operators.
At time of writing, the built-in arithmetic types are all less than 256 bits, so a single byte would be sufficient. Furthermore, x86 shift-instructions use imm8
. Together this suggests the right-hand operand should be an unsigned char
and use of a different type here will require type-conversion.
Is there a "most correct" type to use here? I know the standard is strangely lenient about other aspects of bit-shifting so maybe this is another case of the same?
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 signed right shift operator '>>' uses the sign bit to fill the trailing positions. For example, if the number is positive then 0 will be used to fill the trailing positions and if the number is negative then 1 will be used to fill the trailing positions. In Java, negative numbers are stored as 2's complement.
Bitwise Right Shift Operator Python right shift operator is exactly the opposite of the left shift operator. Then left side operand bits are moved towards the right side for the given number of times. In simple terms, the right side bits are removed.
Logical Right Shifts When shifting right with a logical right shift, the least-significant bit is lost and a 0 is inserted on the other end. 1011 >>> 1 → 0101 1011 >>> 3 → 0001. For positive numbers, a single logical right shift divides a number by 2, throwing out any remainders.
Because the shift operators are defined only for the int, uint, long, and ulong types, the result of an operation always contains at least 32 bits. If the left-hand operand is of another integral type ( sbyte, byte, short, ushort, or char ), its value is converted to the int type, as the following example shows:
The right shift operator >> causes the bits of the left operand to be shifted right by the number of positions specified by the right operand. variable: Allowed data types: byte, int, long. number_of_bits: a number that is < = 32.
In C++, bit shift operators do what their names suggest, shifting bits. According to the program’s requirements, a bitwise shift operator shifts the binary bits left or right.
If a user-defined type T overloads the << or >> operator, the type of the left-hand operand must be T and the type of the right-hand operand must be int. For more information, see the following sections of the C# language specification:
Any integer type can be used as the right operand of a bitwise shift, so long as the value is at least 0 and less than the bit length of the left operand.
This is spelled out in section 6.5.7 p2 and p3 of the C standard regarding Bitwise Shift Operators:
2 Each of the operands shall have integer type
3 The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
So while the range of an unsigned char
should be sufficient to hold any valid value, the right operand will be promoted to int
anyway.
C tends to want to do everything as at least int
, so it would be very surprising if the RHS of <<
and >>
were to be specified as unsigned short
or unsigned char
.
It's hard to imagine why a programmer would ever use long
(or, god help us, long long
) there, but I just tried this code:
int main()
{
int x = 16;
long int y = 2;
int z1 = x << y;
int z2 = x >> y;
printf("%d %d\n", z1, z2);
long long int y2 = 2;
z1 = x << y2;
z2 = x >> y2;
printf("%d %d\n", z1, z2);
}
I compiled it under two compilers, and neither gave any warnings, and both programs printed 64 4
. (Not a conclusive test, but suggestive.)
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