Skip to content

Data Representation

  • Rather than accessing individual bits in memory, most computers use blocks of 8 bits, or bytes, as the smallest addressable unit of memory.

  • 1 byte is represented by 2 hex digits. Ex: 20 (dec) = 14 (hex) = 0001 0100

  • A machine-level program views memory as a very large array of bytes, referred to as virtual memory

  • Every byte of memory is identified by a unique number, known as its address, and the set of all possible addresses is known as the virtual address space

  • Example: assuming 5,000,000 (decimal) = 004C4B40 (hex) to be placed in a double-word (32 bits) variable named var1, little-endian architecture

    Hardware organization .

Hexadecimal notation

  • Hex notation is often used to describing bit patterns.
Hex digit01234567
Decimal value01234567
Binary value00000001001000110100010101100111
---------------------------------------------
Hex digit89ABCDEF
Decimal value89101112131415
Binary value10001001101010111100110111101111

Data sizes

  • Each computer has a word size (nowadays mostly 64-bit = 8 bytes).

  • Typical sizes in C:

    Hardware organization .

  • ISO C99 introduced data types where the data sizes are fixed regardless of compiler. machine settings. Ex: int32_t, int64_t

Addressing and Byte order

  • In virtually all machines, a multi-byte object is stored as a contiguous sequence of bytes, with the address of the object given by the smallest address of the byte used.

  • Consider this example:

    int x = 19088743 // which is 0x01234567
    • A variable x has address 0x100 (aka &x = 0x100), then 4 bytes of x would be stored in memory locations 0x100, 0x101, 0x102, 0x103
    • The ordering of the bytes within the address range 0x100 through 0x103 depends on the type of machine: Hardware organization .

Representing strings

  • A string in C is encoded by an array of characters terminated by the null (having value 0) character.
const char *s = "12345"
// Bytes preresent: 0x 31 32 33 34 35 00 (00 for '\0')

Representing code

  • Consider the following C function
int sum(int x, int y){
return x + y;
}
  • Generated machine code having the following byte representations:

Linux-32 55 89 e5 8b 45 0c 03 45 08 c9 c3
Linux-64 55 48 89 e5 89 7d fc 89 75 f8 03 45 fc c9 c3

  • Binary code is seldom portable across different combinations of machine and OS.

Boolean Algebra

Bit-level operations in C

  • Support bitwise Boolean operations Bitwise example .

Logical operations in C

  • C logical operators: ||, && and !
  • The logical operations treat any nonzero argument as representing true and argument 0 as representing false

Shift operations in C

OperationValue 1Value 2
Argument x[0110 0011][1001 0101]
x << 4[0011 0000][0101 0000]
x >> 4 (logical)[0000 0110][0000 1001]
x >> 4 (arithmetic)[0000 0110][1111 1001]
  • Notes: arithmetic right shift fills the left end with k repetitions of the ost significant bit. (can be 0 or 1)