1k likes | 1.15k Vues
Bit Wizardry. 1. the art of a binary wizard 10. a bit of sorcery 11. bit by bit, it looks like magic 100. a great ability in using bits. Who do we think we are?. We. Dhruv Matani (tall guy) Ashwin Jain (thin guy) Shilp Gupta (fat guy). We. Programmers
E N D
Bit Wizardry • 1. the art of a binary wizard • 10. a bit of sorcery • 11. bit by bit, it looks like magic • 100. a great ability in using bits
We • Dhruv Matani (tall guy) • Ashwin Jain (thin guy) • Shilp Gupta (fat guy)
We • Programmers • Software Developers, Directi • Bit Wizards
You • Programmers • Wish to become Bit Wizards
Bit Wizards • Better programmers • Bits are cool!
Why? • Bits are simple • zero • one
Why? • Bit operations are simple • and • or • not
Why? • 2n is common • Tower of Hanoi • Dynamic Programming • Set cover
Why Tower of Hanoi? • 3n states • Optimal sequence contains • (2n - 1) moves • General case?
Why DP? • Weighted Matching • TSP • Domino Tiling • The list goes on..
Why Set Cover? • Statement • Given ‘n’ items, you are given the cost of covering, each subset of items. Which mutually exclusive subsets to select, such that the union selects all items and the total cost of cover is minimal among all covers.
Why 2n? • n-bits to mask them all,n-bits to find them,n-bits compress them alland in an array bind them
Compress space • Premise • byte - smallest unit of “point-able” memory • byte = 8 bits • Conclusion • Waste not, want not
Faster lookup? • Masks are numbers • Numbers make lightening fast indexes • Think, array!
Are you convinced? • Say yes.
Pay attention! • You too @ashwin + @dhruv!
Rules • If in doubt / distress / disbelief / discomfort / disorientation or difficulty, ask question
Caution • Parenthesize your bit operations • If using Turbo C++ / Borland C++, use long int instead of int
001 • Bit by Bit
bit • A unit of information. • 0 or 1
byte • 1 byte = 8 bits • char (g++)
short int • 1 short int = 16 bits • short int (g++) • int (turbo c++)
int • 1 int = 4 bytes = 32 bits • int (g++) • long int (turbo c++)
long long int • 1 long long int = 64 bits • long long int (g++) • __int64 (visual c++)
and • conjunction • truth table
or • disjunction • truth table
not • negation • truth table
xor • exclusive or / toggle • truth table
bitwise operators • x & y • x | y • ~x • x^y
shift operators • >> • <<
Do it yourself. • wake up, open your computer, try them
Solution • SET(n) A[n >> 5] |= (1 << (n & 31)) • UNSET(n) A[n>>5] &= ~(1 << (n&31))
010 • Things you should know
2n • 1 followed by n 0’s
2n - 1 • 1 repeated n times
-1 • all 1’s
<< • multiply by power of 2
>> • divide by power of 2
& (2n - 1) • remainder on division by 2n
x & 1 • is the number even or odd?
x & (x - 1) • is x a power of 2? • x &= (x-1) removes the least significant set bit! • can you think of immediate uses?
x & (x + 1) • is the binary expansion of x all 1’s?
x & (-x) • smallest power of 2 in the binary expansion of x
x & (-x) • -x = ~x + 1 • 2’s complement notation • Understand this very carefully!