Digits and Bits and Bytes, Oh My!

advertisement
Digits and Bits and Bytes, Oh My!
int x = …;
int y = x & 0xff;
 Similar to boolean and, 1&1=1, else 0
 Clear last three bits: and with 111..111000

0xffffffff << 3
1
0
1
1
0
1
1
1
1
1
1
0
1
1
0
CPS 100, Spring 2010
17.1
Bitwise | and ^: or xor


| is or is at least one one
1
0
1
1
0
1
0
0
0
1
1
0
1
1
1
^ is xor is exactly one one
1
0
1
1
0
1
1
0
0
0
0
1
1
1
0
CPS 100, Spring 2010
17.2
Clearing bits

1457/10 * 10 = 1450 and 1457/100*100 = 1400



How to add bits where they’ve been cleared




What is equivalent for two bits?
What is equivalent for two bits with & and <<
If the cleared bits are zero, can there be a carry on add?
…….000 + three-bit-value = ???
…….000 & three-bit-value = ???
Is there an efficiency issue here?

What do compilers do?
CPS 100, Spring 2010
17.3
CPS 100, Spring 2010
17.4
Download