Reputation: 25768
say, a is an unsigned 32 bit integer.
a << 24
(a & 0xff)<<24
Similiar one, any difference for this two
a >> 16
(a >> 16) & 0xff
Upvotes: 1
Views: 93
Reputation: 11
a << 24
(a & 0xff)<<24
no difference between results, but the latter may avoid influencing overflow flag of register ?
Upvotes: 0
Reputation: 539965
Your first example:
a << 24
(a & 0xff)<<24
In the case of a 32-bit unsigned integer, there is no difference in the result. From the C standard (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. (...)
Your second example
a >> 16
(a >> 16) & 0xff
The seconds line truncates the result to the range 0 .. 255, while the first does not. If you meant the difference between
a >> 16
(a >> 16) & 0xffff
then there is again no difference for unsigned 32-bit integers:
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2. (...)
Note that "unsigned" is important here. For signed integers, the behaviour can be different, implementation-defined, or undefined.
Upvotes: 4
Reputation: 30136
Any difference for these two:
a >> 16
(a >> 16) & 0xff
Yes, you'll get different results for every a > 0x00FFFFFF
.
Upvotes: 0