Frage

Could someone explain me why the following code:

#include <iostream>
#include <bitset>

int main()
{
    unsigned char i = 2;
    std::cout<<std::bitset<8>((~static_cast<unsigned char>(0)) << i)<<std::endl;
    std::cout<<std::bitset<8>((~static_cast<unsigned char>(0)) >> i)<<std::endl;
    return 0;
}

Produces:

11111100
11111111

and not:

11111100
00111111
War es hilfreich?

Lösung

Before ~ is done static_cast<unsigned char>(0) is converted to int (integer promotion happens), so after ~ it becomes all-one bits int. This then is shifted and truncated to 8 bits in bitset.

Andere Tipps

On right-shifts, signed values are zero-filled on if the most significant bit is 0, and one-filled if the most significant bit is 1.

Using unsigned values forces zero-filling on right shifts.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top