I have a specific C bit-shifting scenario that I do not believe is covered on Stack Overflow yet. (If it is, I haven't been able to find it!)
This exercise is using signed int
s as the data type.
Take the value 0x80000000
(which is just a 1 in the most significant bit.)
Shift it right once on a machine using arithmetic right-shifts (which mine does).
Result = 0xC0000000
(1100 0000 in leftmost byte).
Continue shifting it and you should be filling up with ones, from the left to the right.
Result = 0xFFFFFFFF
(All ones.)
However: Try the same example but shift one extra position, all together:
0x80000000 >> 0x00000020
(Right shift 32 times)
Your result? I don't know. My result was not all ones. In fact, I got 0x00000001
which was NOT my desired behavior. Why is this? Is it machine specific?
(Context: Homework assignment limiting my operations to a few bit operators to solve a puzzle. This is an aspect to a puzzle but is far from the entire question.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…