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
Hexadecimal notation
- Hex notation is often used to describing bit patterns.
Hex digit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Decimal value | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Binary value | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 |
------------- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
Hex digit | 8 | 9 | A | B | C | D | E | F |
Decimal value | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
Binary value | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
Data sizes
-
Each computer has a
word size
(nowadays mostly 64-bit = 8 bytes). -
Typical sizes in
C
: -
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:
- A variable
x
has address0x100
(aka&x = 0x100
), then 4 bytes ofx
would be stored in memory locations0x100
,0x101
,0x102
,0x103
- The ordering of the bytes within the address range
0x100
through0x103
depends on the type of machine:
- A variable
Representing strings
- A string in C is encoded by an array of characters terminated by the null (having value 0) character.
Representing code
- Consider the following C function
- 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
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
Operation | Value 1 | Value 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)