Embedded

View Original

An Introduction to Binary and Hexadecimal

Becoming familiar with basic binary and hexadecimal (hex) math will make your career
in embedded systems far more enjoyable. Shifting individual bits around is great when
you need to modify only one or two places. But if you need to modify the whole variable,
hex comes in handy because each digit in hex corresponds to a nibble (four bits) in
binary. (Yes, of course a nibble is half of a byte. Embedded folks like their puns.)

See this content in the original post

Note that with four bits (one hex digit) you can represent 16 numbers, but you can’t
represent the number 16. Many things in embedded systems are zero-based, including
addresses, so they map well to binary or hexadecimal numbers.

A byte is two nibbles, the left one being shifted up four spaces from the other. So 0x80
is (0x8 << 4). A 16-bit word is made up of two bytes, so 0x1234 is (0x12 << 8) + (0x34).
A 32-bit word is 8 characters long in hex but 10 characters long in decimal.

Since memory is generally viewed in hex, some values are used to identify anomalies
in memory. In particular, expect to see (and use) 0xDEADBEEF or 0xDEADC0DE as
an indicator (they are a lot easier to remember in hex than in decimal). Two other
important bytes are 0xAA and 0x55. Because the bits in these numbers alternate, they
are easy to see on an oscilloscope and good for testing when you want to see a lot of
change in your values.


This was taken from a sidebar in my book Making Embedded Systems.