Introduction
Fun with bits
Top coder - fun with bits, the article link is here.
Take some notes:
Use bits of an integer to represent a set. Not only does it produce an order-of-magnitude improvement in both speed and size, it can often simplify code at the same time.
Go over the most popular set manipulations in the following:
Set union
A | B
Set intersection
A & B
Set subtraction
A & ~B
Set negation
ALL_BITS ^ A
Set bit
A |= 1 << bit
Clear bit
A &= ~(1 << bit)
Test bit
(A & 1 << bit) != 0
Extracting every last bit
Counting out the bits
No comments:
Post a Comment