View
215
Download
3
Category
Preview:
Citation preview
1
Bit Operator
Advanced Compiler Laboratory 2
Bit operators
! : invert logical value if value is 0 change to 1, otherwise set to 0
~ : invert all bits ~0x1010 = 0xefef
& : bits ‘and’ operator and
| : ‘or’ operator or
Advanced Compiler Laboratory 3
Bit operators
^ : ‘xor’ operator xor
+ : add << : left bit shifter >> : right bit shifter
warning: it is arithmetic operator( if MSB is 1, go on filling 1 to MSB )
Advanced Compiler Laboratory 4
Our program restrictions
We are only allowed to use the following eight operations( or more fewer op. depending each problem ) ! ~ & ^ | + << >>
any constants longer than 8bits is not allowed max constant is 0xff
Advanced Compiler Laboratory 5
file download
wget http://aces.snu.ac.kr/~jongyoung/lecture/pa01.tar
tar -xvf pa01.tar We modify only bits.c dlc check our program whether coding rule
is correct btest check our ‘bits.c’ correctness
Advanced Compiler Laboratory 6
Goal
Modify functions in bits.c to same result what we expected.
Advanced Compiler Laboratory 7
bitAnd
Using de morgan’s law ~(A and B) = ~A or ~B
Only use the operation ~ and |
Advanced Compiler Laboratory 8
bitXor
Using only the operation & and ~ Similar as bitAnd
Advanced Compiler Laboratory 9
evenBits
Return a word with all even-numbered bits set to 1
Sequence add and bit-shift for some constant.
Careful for constant’s length
Advanced Compiler Laboratory 10
bitMask
First, make 32bit constant that all bits are ‘1’
properly shift If lowbit >= highbit, then the mask should
be all 0’s bitMask(5,3) returns 0x38
Advanced Compiler Laboratory 11
bitParity
Reduce problem’s complexity by folding input variable
Return 1 if x contains an odd number of 0’s. bitParity(5) = 0, bitParity(7) = 1
Advanced Compiler Laboratory 12
fitsBit
Divide input into positive and negative.
Return 1 if x can be represented as an n-bit, two’s complement integer, where 1 <= n <= 32. fitsBit(5,3) returns 0 fitsBit(-4,3) returns 1
Advanced Compiler Laboratory 13
sm2tc
Convert from sign-magnitude to two’s complement where the MSB is the sign bit
Extract sign bit and othersSm2tc(0x80000005) returns -5
Advanced Compiler Laboratory 14
Restriction
Advanced Compiler Laboratory 15
Function Description
Advanced Compiler Laboratory 16
Function Description
Recommended