Binary, Hexadecimal and Signed Numbers
Read register values and convert between binary, hexadecimal and signed representations.
The same quantity can be written with different alphabets. Binary uses two symbols, hexadecimal sixteen; the bit pattern needs a type to tell you what it means.
Each binary position has a power-of-two weight. Starting from the right, those weights are 1, 2, 4, 8 and so on. Add the weights where the bit is one. Hexadecimal groups four bits into one digit, so a byte becomes two hex digits. This makes long register patterns easier to read without changing the underlying bits.
An unsigned n-bit value ranges from zero to 2ⁿ − 1. Eight bits therefore represent 0 through 255. Leading zeros do not change the value but do make the width visible. Width matters when a register has a fixed number of bits or a message reserves exactly two bytes for a field.
In an n-bit two's-complement interpretation, the highest bit has weight −2ⁿ⁻¹. The range becomes −2ⁿ⁻¹ through 2ⁿ⁻¹ − 1. Thus 11111111 means 255 as an unsigned byte and −1 as an eight-bit two's-complement signed value. The bits alone do not identify the interpretation; the type or protocol does.
Endianness describes the order of bytes in a multi-byte value, not the order of decimal digits in a printed number. Sending 0x1234 as little-endian produces byte 0x34 followed by 0x12. Specify byte order explicitly when exchanging data instead of copying a memory representation and hoping the receiver matches.
Read register values and convert between binary, hexadecimal and signed representations.
Core ideas
- Four binary bits correspond to one hex digit.
- A bit pattern needs a width and interpretation.
- Specify byte order in communication formats.
Unsigned range: 0 … 2ⁿ − 1Two's-complement range: −2ⁿ⁻¹ … 2ⁿ⁻¹ − 1Work through a practical example
1Split binary 10110110 into 1011 and 0110.
2These groups are hexadecimal B and 6, giving 0xB6.
3Unsigned decimal value is 128 + 32 + 16 + 4 + 2 = 182.
4As signed eight-bit two's complement, subtract 256: 182 − 256 = −74.
Assuming 0xFF always means −1 or always means 255 without checking the type.