0

I faced with some strange problem. After declaring uint32_t variable with -1 I got 4294967295, that's right. So binary it will looks like

1111 1111 1111 1111 1111 1111 1111 1111

It's 32 bits. But if i'am trying to execute following code, I am taking unexpected behavior.

uint32_t data = (uint32_t) -1;

std::cout << "Before:      " << data << std::endl;
std::cout << "After << 31: " << (data >> 31) << std::endl;
std::cout << "After << 32: " << (data >> 32) << std::endl;

Results are:

Before:      4294967295
After << 31: 1
After << 32: 4294967295

I am using MinGW w64

Megaxela
  • 97
  • 8
  • 1
    See [Arithmetic right shift gives bogus result?](http://stackoverflow.com/q/19636539/1708801) – Shafik Yaghmour Jan 02 '16 at 22:50
  • 1
    Your prompts lie. The prompt says "<<" but the operation is `(data >> 31)`. – Thomas Matthews Jan 02 '16 at 22:52
  • BTW, bits are better viewed in binary or hexadecimal notation. Decimal is a little bit harder to convert to binary. – Thomas Matthews Jan 02 '16 at 22:53
  • 1
    Can you explain what you expect and why? – David Schwartz Jan 02 '16 at 22:55
  • @ShafikYaghmour Ok, I'll see it now. @ThomasMatthews Ye, my mistake. Code was written fast for "presentation". And i agree with you, that much easier to work with bits, using binary or hexademical notations, I am using `python` to convert them. It's easier for me. @DavidSchwartz I expected 0, like `431 >> 10` -> `0`. It's difficult to explain, why am I expected this behavior, but it works with other left values (not -1). Even if I'll try to work with ((uint32_t) -2) it will work – Megaxela Jan 02 '16 at 23:23
  • Just finished analysing `asm` code with `x64dbg` debugger. `shr eax, cl`, when `eax` equals `0xFFFFFFFF` and `cl` equals `0x20` doing nothing. Pretty strange. Well, it only means, that I will use cycles. Thanks anyone. – Megaxela Jan 02 '16 at 23:33

0 Answers0