bitsbytesintegers

Review of Intergers

Video

Bits can represent set

1
2
3
s = { 0 , 3 ,5 ,6}
bits_rep = 0b01101001
position : 76543210
  • This representation is useful when we do File I/O.

  • bits operations can stand for set operation

    bit operation set operation
    & Intersection
    | Union
    ~ Complement
    ^ Symmetric difference

Shift operation

shift operations always throw away the extra bits

Mapping between signed and 2’s complement number

Keep bit representation and reinterpret it

Constants in C

  • by default considered to be signed values
  • Unsigned if have “u”/“U” as suffix
1
2
int a = 1;
unsigned b = 1U; //unsiged int here

Expression evaluation

If there is a mix of signed and unsigned value in a single expression, signed value implicitly cast to unsigned

  • unsigned >= 0 is always true, don’t use this for loop condition

    1
    2
    3
    unsigned a = 0u;               
    printf("%u\n" , --a);
    //4294967295
    1
    2
    //infinite loop
    for (unsigned i = N - 1; i >= 0 ; i--) ;
  • Proper way to use unsigned

    1
    2
    3
    4
    unsigned i ;
    for ( i = N - 1 ; i < N ; i--) ;
    //break after i = 0
    //Note that N shouldn't be negative signed
  • See more at Robert Seacord, Secure Coding in C and C++

Truncation signed numbers

Say that signed number x has bit representation of $[x_{w-1} , x_{w-2} , …. , x_0]$

x’, standing for truncated x to k bits, has the bit representation of $[x_{k-1} , x_{k-2} , …,x_0]$
$$
x = -x_{w-1}2^{w-1}+x_{w-2}2^{w-2}+…+x_02^0
$$

$$
x’ =-x_{k-1}2^{k-1} + x_{k-2}2^{k-2} +…+x_02^0
$$

$$
x \mod 2^k = x_{k-1}2^{k-1} + x_{k-2}2^{k-2} +x_02^0
$$

$$
x \mod 2 ^k -x_{k-1}2^k=x’
$$

Unfortunately, we can not determine $x_{k-1}$ with a breeze, so after moduloing $2^k$

We first view interpret the bits as unsigned , then convert it to signed

Division rounding

division should always round towards 0

A useful approximation

$$
2^{10} = 1024 =1000 = 10^3
$$

What is a ‘word size’ ?

the definition is ambiguous, you can just take it as the size of a pointer(address)

group $x$ bits together as a word, $x$ is a word size

Word oriented memory organization

arrangement

address of successive words differ by 4(32 bits) or 8(64 bits)