Tuesday, February 19, 2008

CSE2031 Lecture 11

    #include<stdio.h>


 

int main(int argc, char* argv[])

{


 

return 0;

]


 

-------------------------------------------------------------

command line arguments

-------------------------------------------------------------

args can be accessed just like a normal allocated array

printf("%s\n", argx[2]);


 

-------------------------------------------------------------

bit operations

-------------------------------------------------------------


 

used to be used to pack and unpack things into small space, but space is cheap now.


 

operating on bits


 

logical operators (unary) bitwise complement ~

    bitwise AND    &

    bitwise XOR    `

    bitwise OR    |


 

shift operators

    left shift        <<

    right shift        >>


 

do not confuse & with &&

     or | with ||


 

right shift shifts in ?? on left end

for unsigned, zeros shifted on the left end

for signed, machine dependent

    -shift in zeros

    -shit in sign bits

int    a=1 << 31; /* shift 1 to high bit*/

unsinged    b = 1 << 31;


 

expn        representation

a        10000000 00000000 00000000 00000000

a >> 3        11110000 00000000 00000000 00000000

b        10000000 00000000 00000000 00000000

b >> 3        00010000 00000000 00000000 00000000


 

to turn bits on , use OR and 1's

v | 3 << 2

v | 00000000 00001100

has 3rd and 4th bit on, others as in v


 

to turn bits off, use AND and 0's

    v & ~(3<<2)

    v & 11111111 11110011

    has 3rd and 4th bit off, others as in v


 

masks


 

    -a string of bits used to extract bits from another expression

    -to find value of a particular bit in an expression

    - use a mask that is 1 at that bit and 0


 

otherwise


 

    1 << 2 as mask to query 3rd bit in v

    (v & (1 << 2) ) ? 1 : 0


 

    has value 1 if 3rd bit in v is 1 and 0 otherwise


 

bit representation of 255

    00000000 00000000 00000000 11111111

    

    0    --> all 0's

    ~0     --> all 1's

    ~0<<k     --> 111...11 000..0

~0(~0<<k)<<r     --> 000..0 11111111 000.00


 

No comments: