Is right shifting unsigned types in c++ implementation defined

Viewed 86

I know that right shifting a signed type in c++ (such as an int) is implementation defined: some machines will perform a logical right shift, and other machines will perform an arithmetic right shift.

I made the following code

#include <iostream>
#include <bitset>

int main()
{
    unsigned int a = 1 << 31;  // It is an unsigned int on purpose
    for (int i = 0; i < 32; i++){
        std::cout << std::bitset<(32)>(a >> i) << "\n";
    }
}

and the output was

10000000000000000000000000000000
01000000000000000000000000000000
00100000000000000000000000000000
...
00000000000000000000000000000010
00000000000000000000000000000001

which means that my machine performs logical right shifts to an unsigned int. I was wondering if this is always the case: is right shifting an unsigned int (or any other unsigned types) implementation defined, or is right shifting an unsigned int always a logical right shift?

0 Answers
Related