Binary basics
MSB, LSB and integer overflow
What the most and least significant bits tell you, how signed integers use the MSB, and why 127 + 1 can equal -128.
In a binary number, the most significant bit (MSB) is the leftmost bit and the least significant bit (LSB) is the rightmost. The MSB is worth the most: in the byte 10110010 it is worth 128. The LSB is worth 1, so it tells you at a glance whether the number is odd or even. In signed integers the MSB also works as the sign bit, which is why adding 1 to the largest value a type can hold can suddenly give you a negative number. That is integer overflow, and this guide covers all three ideas because each one explains the next.
Key takeaways
| The MSB is the leftmost bit with the largest place value, 128 in a byte. The LSB is the rightmost, worth 1. | |
| The LSB is 1 for odd numbers and 0 for even ones, so n & 1 is a quick odd or even test. | |
| In a signed two's complement integer the MSB is the sign bit, so 11111111 is 255 unsigned but -1 signed. | |
| Integer overflow drops the bit that carries past the MSB, so 8-bit 255 + 1 is 0 and signed 127 + 1 is -128. | |
| Prevent overflow with wider types, a check such as a > MAX - b before adding, or your language's checked math helpers. |
Type a number and pick a size to see its bits, with the MSB and LSB marked, and what happens when the value no longer fits.
MSB and LSB in a byte
Each position in a binary number has a place value that doubles from right to left: 1, 2, 4, 8 and so on. In an 8-bit byte the left end is worth 128 and the right end is worth 1.

Take 10110010. The MSB is 1, so the value is at least 128. Adding the places that hold a 1 gives 128 + 32 + 16 + 2 = 178. The LSB is 0, so 178 is even. Flip the MSB and the value drops by 128 to 50. Flip the LSB and it changes by only 1. That difference in weight is all "most significant" and "least significant" mean. The binary to decimal converter shows the same place-value sum for any number.
The same words apply at any width. In a 16-bit number the MSB is worth 32,768, and in a 32-bit number it is worth 2,147,483,648. Some datasheets number the bits from 0 at the LSB up to 7 or 15 at the MSB, so "bit 7" of a byte is its MSB.
What the LSB tells you
The LSB is the ones place, so it is 1 for every odd number and 0 for every even one. That gives a fast odd or even test in any language: AND the number with 1 and look at the result.
for n in [178, 179]:
print(n, format(n, '08b'), 'odd' if n & 1 else 'even')
178 10110010 even
179 10110011 odd
Because changing the LSB changes a value by only 1, it is also where small changes hide. In an image, flipping the LSB of a pixel's red value from 200 to 201 is invisible to the eye, which is the idea behind LSB steganography, hiding a message in the low bits of a picture. In sensor readings the LSB is the part most affected by electrical noise, which is why the last digit of a reading often jumps around.
What the MSB tells you
In an unsigned number the MSB says whether the value is in the top half of the range: for a byte, 128 or more. In a signed number it says whether the value is negative. Most computers store signed integers in two's complement, where the MSB counts as a negative weight instead of a positive one. In 8 bits it is worth -128 instead of +128.
| Bits | As unsigned | As signed (two's complement) |
|---|---|---|
00000001 | 1 | 1 |
01111111 | 127 | 127 |
10000000 | 128 | -128 |
10110010 | 178 | -78 |
11111111 | 255 | -1 |
The bits are the same in both columns. Only the program's reading of the MSB changes. 11111111 is 255 to an unsigned byte and -1 to a signed one, which is why the type of a variable matters as much as its bits. The two's complement calculator converts between the two readings and shows the steps.
MSB first and LSB first
When bits travel one at a time down a wire, the sender and receiver must agree which end goes first. SPI and I2C usually send the MSB first. A standard serial port (UART) sends the LSB first. If a device returns garbage that looks like your data reversed bit by bit, a mismatched bit order is the likely cause.
Do not confuse this with byte order. "MSB" sometimes also means most significant byte, the high byte of a multi-byte number, and whether that byte is stored first is a question of endianness. Our guide to big endian and little endian covers byte order.
Signed vs unsigned integers
An unsigned integer uses every bit for the size of the number, so it can only hold zero and positive values. A signed integer gives the MSB the sign role, which halves the positive range and adds the same number of negative values.
| Size | Unsigned range | Signed range |
|---|---|---|
| 8-bit | 0 to 255 | -128 to 127 |
| 16-bit | 0 to 65,535 | -32,768 to 32,767 |
| 32-bit | 0 to 4,294,967,295 | -2,147,483,648 to 2,147,483,647 |
| 64-bit | 0 to 18,446,744,073,709,551,615 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Use unsigned types for things that can never be negative and where the bit patterns matter, such as bytes, flags, colors and hashes. Use signed types for anything you might subtract, because subtracting below zero is where unsigned values cause the worst bugs, as the next sections show. The powers of 2 chart lists where each of these limits comes from.
What is integer overflow?
Integer overflow happens when the result of a calculation needs more bits than the type has. The extra bit that would carry out past the MSB has nowhere to go and is dropped, so the value wraps around to the other end of the range.

Here is the same addition in an 8-bit unsigned and an 8-bit signed variable. Python's own integers never overflow, so the example uses the fixed-size C types from ctypes:
import ctypes
print(ctypes.c_uint8(255 + 1).value)
print(ctypes.c_int8(127 + 1).value)
print(ctypes.c_int32(2147483647 + 1).value)
0
-128
-2147483648
- Unsigned: 255 is
11111111. Adding 1 gives1 00000000, nine bits. The ninth is dropped and 0 is left. - Signed: 127 is
01111111. Adding 1 gives10000000, which fits in 8 bits, but its MSB is now 1, so it reads as -128. - The same happens at 32 bits: one more than 2,147,483,647 becomes -2,147,483,648.
JavaScript numbers are floating point doubles, but typed arrays and bitwise operators use fixed-size integers and wrap the same way:
console.log(new Uint8Array([256])[0], new Int8Array([128])[0]);
console.log((2147483647 + 1) | 0);
0 -128
-2147483648
Integer underflow
Going below the bottom of the range wraps to the top. An unsigned byte holding 0 minus 1 becomes 255, and an unsigned 32-bit counter becomes 4,294,967,295. This is the classic bug when code computes a length or a count with unsigned types: items - 1 on an empty list gives a huge number instead of -1, and a loop that should run zero times runs four billion times.
Integer overflow in real systems
Overflow bugs have caused some well-known failures, and one is still on its way.
- The year 2038 problem. Many systems store time as a signed 32-bit count of seconds since 1 January 1970. That count reaches 2,147,483,647 at 03:14:07 UTC on 19 January 2038, and one second later it wraps to a date in December 1901. Modern systems use a 64-bit count, but old embedded devices and file formats still carry the 32-bit one.
- Ariane 5, 1996. Thirty-seven seconds after its first launch, the rocket's guidance software converted a 64-bit floating point value into a 16-bit signed integer. The value was larger than 32,767, the conversion overflowed, and the rocket broke up.
- YouTube view counts, 2014. A music video passed 2,147,483,647 views, the largest signed 32-bit number, and YouTube moved its counters to 64-bit integers.
How to detect and prevent integer overflow
How a language reacts to overflow decides how you guard against it.
| Language | What happens on overflow |
|---|---|
| Python | never overflows, int grows as needed |
| JavaScript | numbers lose precision above 253, bitwise operations wrap at 32 bits, BigInt never overflows |
| Java, C#, Go | wraps around silently (C# can throw inside a checked block) |
| C, C++ | unsigned types wrap; signed overflow is undefined behavior, so the compiler may assume it never happens |
| Rust | panics in debug builds, wraps in release builds unless you use checked or wrapping methods |
Three habits prevent most overflow bugs:
- Pick a type with room to spare. If a counter might pass 2 billion, use a 64-bit integer from the start.
- Check before the operation, not after. For two positive numbers,
a + boverflows whena > MAX - b, and that test cannot overflow itself. - Use the checked helpers your language provides:
Math.addExact()in Java,checked_add()in Rust,__builtin_add_overflow()in GCC and Clang, or acheckedblock in C#.
When you are working out by hand what a value will wrap to, the binary calculator shows the carry out of the top bit, and the two's complement calculator shows the signed reading for any width.
Questions people ask
What is the most significant bit?
The leftmost bit of a binary number, with the largest place value. In an 8-bit byte it is worth 128, and in a signed integer it is the sign bit.
What is the least significant bit?
The rightmost bit, worth 1. It is 1 for odd numbers and 0 for even numbers.
How do I find the MSB and LSB of a number?
Write the number in binary at its full width. The leftmost bit is the MSB and the rightmost is the LSB. For 178 in 8 bits, 10110010, the MSB is 1 and the LSB is 0.
What is integer overflow?
It happens when a result needs more bits than its type has. The extra high bit is lost and the value wraps around, so an 8-bit 255 + 1 becomes 0 and a signed 127 + 1 becomes -128.
What is the difference between signed and unsigned integers?
Unsigned integers use every bit for the value and hold only zero and positive numbers. Signed integers use the MSB as a sign, so an 8-bit signed value runs from -128 to 127 instead of 0 to 255.
Does Python have integer overflow?
Not for its own int type, which grows as large as memory allows. Fixed-size types from NumPy, ctypes or struct do wrap around.
Keep reading
All posts
Binary basicsFloating point numbers explained: why 0.1 + 0.2 is not 0.3
A floating point number is scientific notation in binary. See why 0.1 + 0.2 is 0.30000000000000004, how precise floats are, float vs double, and how to compare floats and handle money.9 min read
Binary basicsWhat is hexadecimal? The base 16 number system explained
Hexadecimal is base 16, with digits 0 to 9 and A to F. See how place values work, why programmers use hex for bytes, the values worth knowing and how octal compares.7 min read
Binary basics
