All courses / Data Structures and Algorithms for Game Devs / Bits and bytes
Negative numbers in binary
Demo lesson, about 14 min, +25 XP
Games are full of numbers below zero. Buying a sword changes Nico's gold by -25. A skeleton two tiles to the left of the hero sits at x = -2. Memory has no minus sign, though. It only has bits. So how does a computer store -25?
The last two lessons left two puzzles open. In Bit flags, ~Burning printed -3. In Shifting bits, 1 << 31 printed -2147483648. You also saw int.MaxValue + 1 turn into int.MinValue back in C# Basics. All of them come from one rule, and this lesson is about that rule.
Read the rest of this lesson and run the code in your browser.
Previous: Shifting bits Next: Under the hood: the level 256 glitch