Data Representation

Created

There are may ways to represent numbers. Consider...

  • Roman Numerals

  • Arabic Numerals

  • Base 2

  • Base 8, or Octal Numbers, are represented by grouping three binary digits.

    • Used in chmod 600 foo.txt
  • Hexadecimal groups 4 binary digits for base 16. Uses numbers 0-9 and letters a-f.

Bytes

On modern computers, data is accessed in chunks of 8 bits, which is the same as 1 byte.

  • E.g., a char data type in C is 1 byte.

Larger chunks of data (called "words") are formed from multiple bytes (which are all powers of two because it doubles the number of bits):

  • 2 bytes = 16 bits
  • 4 bytes = 32 bits
  • 8 bytes = 64 bits

Modern CPUs have instructions for doing operations on each word-sized data values.

C Data Types

C is supposed to be one step above assembly language.

The "primitive" C data types typically map onto machine word sizes (not consistent across different machines and compiler, though).

"Typical" representations of C data types include (data type | bytes used on 32-bit systems | bytes used on 64-bit systems):

  • char | 1 | 1
  • short | 2 | 2
  • int | 4 | 4
  • long | 4 | 8

Note the difference on the last line! The reason long is different is because long is represented using pointers, and on 64-bit systems, pointers are larger.

Portable Integer Types stdint.h header files provide protable integer types providing an exact number of bits: int32_t, uint32_t, int64_t, uint64_t, etc

If you write code for different systems, be careful when using constants.

  • For example, 0x100000000UL (232) is likely to be a valid on a 64-bit system but not on a 32-bit system
  • Note: the “UL” suffix means “unsigned long”