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 [xw1,xw2,.,x0]

x’, standing for truncated x to k bits, has the bit representation of [xk1,xk2,,x0]
x=xw12w1+xw22w2++x020

x=xk12k1+xk22k2++x020

xmod2k=xk12k1+xk22k2+x020

xmod2kxk12k=x

Unfortunately, we can not determine xk1 with a breeze, so after moduloing 2k

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

Division rounding

division should always round towards 0

A useful approximation

210=1024=1000=103

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

arrangementarrangement

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