1 bit operator. advanced compiler laboratory2 bit operators ! : invert logical value if value is 0...

16
1 Bit Operator

Upload: brent-walters

Post on 12-Jan-2016

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

1

Bit Operator

Page 2: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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

Page 3: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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 )

Page 4: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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

Page 5: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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

Page 6: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

Advanced Compiler Laboratory 6

Goal

Modify functions in bits.c to same result what we expected.

Page 7: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

Advanced Compiler Laboratory 7

bitAnd

Using de morgan’s law ~(A and B) = ~A or ~B

Only use the operation ~ and |

Page 8: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

Advanced Compiler Laboratory 8

bitXor

Using only the operation & and ~ Similar as bitAnd

Page 9: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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

Page 10: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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

Page 11: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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

Page 12: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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

Page 13: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

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

Page 14: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

Advanced Compiler Laboratory 14

Restriction

Page 15: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

Advanced Compiler Laboratory 15

Function Description

Page 16: 1 Bit Operator. Advanced Compiler Laboratory2 Bit operators ! : invert logical value if value is 0 change to 1, otherwise set to 0 ~ : invert all bits

Advanced Compiler Laboratory 16

Function Description