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.
Strange behaviour with right bit shift
-
30-09-2022 - |
Вопрос
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
Решение
Другие советы
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.
Не связан с StackOverflow