Bit shifting an int 32 times in C

bit-shift, c

Solution

It's undefined behavior

Why doesn't left bit-shift, "<<", for 32-bit integers work as expected when used more than 32 times?

which means you should avoid it like the plague. Here's some links including "What every C programmer should know about undefined behavior": http://lwn.net/Articles/511767/

Problem

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.)

Original source

Related problems