Download presentation
Presentation is loading. Please wait.
Published byBrent Walters Modified over 8 years ago
1
1 Bit Operator
2
Advanced Compiler Laboratory2 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
3
Advanced Compiler Laboratory3 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 )
4
Advanced Compiler Laboratory4 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
5
Advanced Compiler Laboratory5 file download wget http://aces.snu.ac.kr/~jongyoung/lect ure/pa01.tar http://aces.snu.ac.kr/~jongyoung/lect ure/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
6
Advanced Compiler Laboratory6 Goal Modify functions in bits.c to same result what we expected.
7
Advanced Compiler Laboratory7 bitAnd Using de morgan’s law ~(A and B) = ~A or ~B Only use the operation ~ and |
8
Advanced Compiler Laboratory8 bitXor Using only the operation & and ~ Similar as bitAnd
9
Advanced Compiler Laboratory9 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
10
Advanced Compiler Laboratory10 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
11
Advanced Compiler Laboratory11 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
12
Advanced Compiler Laboratory12 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
13
Advanced Compiler Laboratory13 sm2tc Convert from sign-magnitude to two’s complement where the MSB is the sign bit Extract sign bit and others Sm2tc(0x80000005) returns -5
14
Advanced Compiler Laboratory14 Restriction
15
Advanced Compiler Laboratory15 Function Description
16
Advanced Compiler Laboratory16 Function Description
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.