Hyston blog
About • Archive • RSS

Biteshift

January 11, 2020

I started to participate in Advent of code 2020. First couple tasks were easy, but after a while it keep getting harder and took more time, that I can manage on average december day. Today is 8 day and I done only 14 of them and want to share one bug, that I did found solving last one. I am pretty familiar with bitwise operators, espacially shift:

1 << 1; //result it 2 (bin: 10)
1 << 3; //result it 8 (bin: 1000)

However, in day 14 bitmask is 36 bit length. If you shift too much, it should be cut. Alright, I thought, lets use unsined 64-bit integers.
ulong a = 1 << 32;
Because currently I dont have a debugger1, and it worked flawlessly with smaller values, I havent noticed an error here. Documentation said

Because the shift operators are defined only for the int, uint, long, and ulong types, the result of an operation always contains at least 32 bits and I naturally assumed, that if I define left hand operand as 64bit, then result would be also 64. That was correct, but before it


  1. VS Code support for M1 macs is incomplete, as I'm afraid, until .net 6 😣


Next entry → ← Prev entry