Bit Wizardry
description
Transcript of Bit Wizardry
![Page 1: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/1.jpg)
1. the art of a binary wizard10. a bit of sorcery11. bit by bit, it looks like magic100. a great ability in using bits
1. the art of a binary wizard10. a bit of sorcery11. bit by bit, it looks like magic100. a great ability in using bits
Bit WizardryBit Wizardry
![Page 2: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/2.jpg)
Who do we think we are?Who do we think we are?
![Page 3: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/3.jpg)
WeWe
•Dhruv Matani (tall guy)
•Ashwin Jain (thin guy)
•Shilp Gupta (fat guy)
•Dhruv Matani (tall guy)
•Ashwin Jain (thin guy)
•Shilp Gupta (fat guy)
![Page 4: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/4.jpg)
WeWe
•Programmers
•Software Developers, Directi
•Bit Wizards
•Programmers
•Software Developers, Directi
•Bit Wizards
![Page 5: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/5.jpg)
Who do we think you are?
Who do we think you are?
![Page 6: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/6.jpg)
YouYou
•Programmers
•Wish to become Bit Wizards
•Programmers
•Wish to become Bit Wizards
![Page 7: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/7.jpg)
Bit WizardsBit Wizards
•Better programmers
•Bits are cool!
•Better programmers
•Bits are cool!
![Page 8: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/8.jpg)
Why?Why?
![Page 9: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/9.jpg)
Why?Why?
•Bits are simple
•zero
•one
•Bits are simple
•zero
•one
![Page 10: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/10.jpg)
Why?Why?
•Bit operations are simple
•and
•or
•not
•Bit operations are simple
•and
•or
•not
![Page 11: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/11.jpg)
Why?Why?
•2n is common
•Tower of Hanoi
•Dynamic Programming
•Set cover
•2n is common
•Tower of Hanoi
•Dynamic Programming
•Set cover
![Page 12: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/12.jpg)
Why Tower of Hanoi?Why Tower of Hanoi?
•3n states
•Optimal sequence contains
•(2n - 1) moves
•General case?
•3n states
•Optimal sequence contains
•(2n - 1) moves
•General case?
![Page 13: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/13.jpg)
Why DP?Why DP?
•Weighted Matching
•TSP
•Domino Tiling
•The list goes on..
•Weighted Matching
•TSP
•Domino Tiling
•The list goes on..
![Page 14: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/14.jpg)
Why Set Cover?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.
•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.
![Page 15: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/15.jpg)
Why 2n?Why 2n?
•n-bits to mask them all,n-bits to find them,n-bits compress them alland in an array bind them
•n-bits to mask them all,n-bits to find them,n-bits compress them alland in an array bind them
![Page 16: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/16.jpg)
What the cuss was that?What the cuss was that?
![Page 17: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/17.jpg)
Compress spaceCompress space
•Premise
•byte - smallest unit of “point-able” memory
•byte = 8 bits
•Conclusion
•Waste not, want not
•Premise
•byte - smallest unit of “point-able” memory
•byte = 8 bits
•Conclusion
•Waste not, want not
![Page 18: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/18.jpg)
Faster lookup?Faster lookup?
•Masks are numbers
•Numbers make lightening fast indexes
•Think, array!
•Masks are numbers
•Numbers make lightening fast indexes
•Think, array!
![Page 19: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/19.jpg)
Are you convinced?Are you convinced?
•Say yes.•Say yes.
![Page 20: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/20.jpg)
Pay attention!Pay attention!
•You too @ashwin + @dhruv!•You too @ashwin + @dhruv!
![Page 21: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/21.jpg)
RulesRules
•If in doubt / distress / disbelief / discomfort / disorientation or difficulty, ask question
•If in doubt / distress / disbelief / discomfort / disorientation or difficulty, ask question
![Page 22: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/22.jpg)
CautionCaution
•Parenthesize your bit operations
•If using Turbo C++ / Borland C++, use long int instead of int
•Parenthesize your bit operations
•If using Turbo C++ / Borland C++, use long int instead of int
![Page 23: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/23.jpg)
001001
Bit by BitBit by Bit
![Page 24: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/24.jpg)
bitbit
•A unit of information.
•0 or 1
•A unit of information.
•0 or 1
![Page 25: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/25.jpg)
bytebyte
•1 byte = 8 bits
•char (g++)
•1 byte = 8 bits
•char (g++)
![Page 26: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/26.jpg)
short intshort int
•1 short int = 16 bits
•short int (g++)
•int (turbo c++)
•1 short int = 16 bits
•short int (g++)
•int (turbo c++)
![Page 27: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/27.jpg)
intint
•1 int = 4 bytes = 32 bits
•int (g++)
•long int (turbo c++)
•1 int = 4 bytes = 32 bits
•int (g++)
•long int (turbo c++)
![Page 28: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/28.jpg)
long long intlong long int
•1 long long int = 64 bits
•long long int (g++)
•__int64 (visual c++)
•1 long long int = 64 bits
•long long int (g++)
•__int64 (visual c++)
![Page 29: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/29.jpg)
andand
•conjunction
•truth table
•conjunction
•truth table
![Page 30: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/30.jpg)
oror
•disjunction
•truth table
•disjunction
•truth table
![Page 31: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/31.jpg)
notnot
•negation
•truth table
•negation
•truth table
![Page 32: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/32.jpg)
xorxor
•exclusive or / toggle
•truth table
•exclusive or / toggle
•truth table
![Page 33: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/33.jpg)
bitwise operatorsbitwise operators
•x & y
•x | y
•~x
•x^y
•x & y
•x | y
•~x
•x^y
![Page 34: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/34.jpg)
shift operatorsshift operators
•>>
•<<
•>>
•<<
![Page 35: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/35.jpg)
Do it yourself.Do it yourself.
wake up, open your computer, try themwake up, open your computer, try them
![Page 36: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/36.jpg)
Print the input in binary using bitwise operators.Print the input in binary using bitwise operators.
![Page 37: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/37.jpg)
Sieve the primes < 1024 using an array of 32 ints
only!
Sieve the primes < 1024 using an array of 32 ints
only!
![Page 38: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/38.jpg)
SolutionSolution
•SET(n) A[n >> 5] |= (1 << (n & 31))
•UNSET(n) A[n>>5] &= ~(1 << (n&31))
•SET(n) A[n >> 5] |= (1 << (n & 31))
•UNSET(n) A[n>>5] &= ~(1 << (n&31))
![Page 39: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/39.jpg)
010010
Things you should knowThings you should know
![Page 40: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/40.jpg)
2n2n
•1 followed by n 0’s•1 followed by n 0’s
![Page 41: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/41.jpg)
2n - 12n - 1
•1 repeated n times•1 repeated n times
![Page 42: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/42.jpg)
-1-1
•all 1’s•all 1’s
![Page 43: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/43.jpg)
<<<<
•multiply by power of 2•multiply by power of 2
![Page 44: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/44.jpg)
>>>>
•divide by power of 2•divide by power of 2
![Page 45: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/45.jpg)
& (2n - 1)& (2n - 1)
•remainder on division by 2n•remainder on division by 2n
![Page 46: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/46.jpg)
x & 1x & 1
•is the number even or odd?•is the number even or odd?
![Page 47: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/47.jpg)
x & (x - 1)x & (x - 1)
•is x a power of 2?
•x &= (x-1) removes the least significant set bit!
•can you think of immediate uses?
•is x a power of 2?
•x &= (x-1) removes the least significant set bit!
•can you think of immediate uses?
![Page 48: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/48.jpg)
x & (x + 1)x & (x + 1)
•is the binary expansion of x all 1’s?•is the binary expansion of x all 1’s?
![Page 49: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/49.jpg)
x & (-x)x & (-x)
•smallest power of 2 in the binary expansion of x
•smallest power of 2 in the binary expansion of x
![Page 50: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/50.jpg)
x & (-x)x & (-x)
•-x = ~x + 1
•2’s complement notation
•Understand this very carefully!
•-x = ~x + 1
•2’s complement notation
•Understand this very carefully!
![Page 51: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/51.jpg)
~x & (x + 1)~x & (x + 1)
•isolate the rightmost 0•isolate the rightmost 0
![Page 52: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/52.jpg)
swap(x,y)swap(x,y)
•x ^= y ^= x ^= y•x ^= y ^= x ^= y
![Page 53: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/53.jpg)
Do it yourselfDo it yourself
do you have your pens and pencils?do you have your pens and pencils?
![Page 54: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/54.jpg)
Given a number x, find the next higher number with the same
number of set bits.
Given a number x, find the next higher number with the same
number of set bits.
![Page 55: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/55.jpg)
011011
Binary searching on bitsBinary searching on bits
![Page 56: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/56.jpg)
BS!?BS!?
•Calculate the number of set bits in a number.
•How many operations can you do it in?
•Most probably O(log n) = O(number of bits)
•Calculate the number of set bits in a number.
•How many operations can you do it in?
•Most probably O(log n) = O(number of bits)
![Page 57: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/57.jpg)
Huh!?Huh!?
•Lets do it in O(log of the number of bits).
•O(log log n) !?
•Lets do it in O(log of the number of bits).
•O(log log n) !?
![Page 58: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/58.jpg)
DefinitionsDefinitions
•Given x. (Assume it has 32 bits)•Given x. (Assume it has 32 bits)
![Page 59: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/59.jpg)
Step 1Step 1
•X = (X & 0x55555555) + ((x & 0xAAAAAAAA) >> 1)
•X = (X & 0x55555555) + ((x & 0xAAAAAAAA) >> 1)
![Page 60: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/60.jpg)
Step 2Step 2
•X = (X & 0x33333333) + ((x & 0xcccccccc) >> 2)
•X = (X & 0x33333333) + ((x & 0xcccccccc) >> 2)
![Page 61: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/61.jpg)
Step 3Step 3
•X = (X & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4)
•X = (X & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4)
![Page 62: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/62.jpg)
Step 4Step 4
•X = (X & 0x00ff00ff) + ((x & 0xff00ff00) >> 8)
•X = (X & 0x00ff00ff) + ((x & 0xff00ff00) >> 8)
![Page 63: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/63.jpg)
Step 5Step 5
•X = (X & 0x0000ffff) + ((x & 0xffff0000) >> 16)
•X = (X & 0x0000ffff) + ((x & 0xffff0000) >> 16)
![Page 64: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/64.jpg)
EndEnd
•return x•return x
![Page 65: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/65.jpg)
Beyond the horizonBeyond the horizon
•Think of the several possibilities
•Compute the parity of the number of 1’s
•Reverse the bits of a number
•Think of the several possibilities
•Compute the parity of the number of 1’s
•Reverse the bits of a number
![Page 66: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/66.jpg)
Do it yourselfDo it yourself
the s**t has hit the fan!the s**t has hit the fan!
![Page 67: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/67.jpg)
Given x, find the highest power of 2, less than x
Given x, find the highest power of 2, less than x
![Page 68: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/68.jpg)
SolutionSolution
•x |= x >> 1x |= x >> 2x |= x >> 4x |= x >> 8x |= x >> 16
•return x - (x >> 1)
•x |= x >> 1x |= x >> 2x |= x >> 4x |= x >> 8x |= x >> 16
•return x - (x >> 1)
![Page 69: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/69.jpg)
100100
Invention of maskingInvention of masking
![Page 70: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/70.jpg)
The IdeaThe Idea
•You know set
•You know subset
•Lets number subsets
•You know set
•You know subset
•Lets number subsets
![Page 71: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/71.jpg)
The IdeaThe Idea
•label items of a set from {0, 1, 2, 3 ... (n-1)}
•n items = 2n subsets
•lets number subsets from 0 to 2n - 1
•label items of a set from {0, 1, 2, 3 ... (n-1)}
•n items = 2n subsets
•lets number subsets from 0 to 2n - 1
![Page 72: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/72.jpg)
The IdeaThe Idea
•Subset number ‘x’ contains item numbered ‘i’ iff bit ‘i’ is set in ‘x’, and vice versa
•Subset number ‘x’ contains item numbered ‘i’ iff bit ‘i’ is set in ‘x’, and vice versa
![Page 73: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/73.jpg)
Get it?Get it?
•x is the mask of the subset
•directly maps to a subset
•an iteration from 0 to 2n - 1 iterates over all subsets!
•x is the mask of the subset
•directly maps to a subset
•an iteration from 0 to 2n - 1 iterates over all subsets!
![Page 74: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/74.jpg)
RealizeRealize
•| = set union
•& = set intersection
•-1 ^ A = set negation
•| = set union
•& = set intersection
•-1 ^ A = set negation
![Page 75: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/75.jpg)
Think!Think!
•Set subtraction
•Adding
•Deleting
•Testing set participation
•Set subtraction
•Adding
•Deleting
•Testing set participation
![Page 76: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/76.jpg)
Dynamic ProgrammingDynamic Programming
•Weighted Matching
•A group of ‘n’ men and ‘n’ women participate in a marriage fare
•A matrix depicts the cost of marriage between ‘i’th guy and ‘j’th gal
•Find the best pairs, such that the total costs of all marriages is the least!
•Weighted Matching
•A group of ‘n’ men and ‘n’ women participate in a marriage fare
•A matrix depicts the cost of marriage between ‘i’th guy and ‘j’th gal
•Find the best pairs, such that the total costs of all marriages is the least!
![Page 77: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/77.jpg)
ConcentrateConcentrate
a presentation can only say as much!a presentation can only say as much!
![Page 78: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/78.jpg)
UnderstandUnderstand
•A matrix of numbers
•Selection of items
•exactly 1 in each row
•exactly 1 in each column
•A matrix of numbers
•Selection of items
•exactly 1 in each row
•exactly 1 in each column
![Page 79: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/79.jpg)
A brute force approachA brute force approach
•n!
•Consider all permutations
•n!
•Consider all permutations
![Page 80: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/80.jpg)
A faster brute forceA faster brute force
•O(n 2n)
•Each mask selects a subset of columns
•And as many rows as columns (from top)
•O(n 2n)
•Each mask selects a subset of columns
•And as many rows as columns (from top)
![Page 81: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/81.jpg)
Lets chalk itLets chalk it
![Page 82: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/82.jpg)
Home workHome work
•Think / read about the brute force solution to Traveling Sales Person (TSP)
•In O(n 2n) space, this faster brute force handles instances up to n = 20, quickly!
•Think / read about the brute force solution to Traveling Sales Person (TSP)
•In O(n 2n) space, this faster brute force handles instances up to n = 20, quickly!
![Page 83: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/83.jpg)
Home workHome work
•Then solve
•PermRLE
•GCJ 2008, Round 2, Problem D
•Which permutation of blocks of 16 characters, allow for the smallest Run Length Encoding of a huge string!
•Then solve
•PermRLE
•GCJ 2008, Round 2, Problem D
•Which permutation of blocks of 16 characters, allow for the smallest Run Length Encoding of a huge string!
![Page 84: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/84.jpg)
101101
Set coverSet cover
![Page 85: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/85.jpg)
The ProblemThe Problem
•Given a mask, how do you calculate the masks that depict the subsets of this mask.
•Given a mask, how do you calculate the masks that depict the subsets of this mask.
![Page 86: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/86.jpg)
The SolutionThe Solution
•for(nmask = mask; nmask > 0; nmask = mask & (nmask - 1)) {
• // use nmask
•}
•for(nmask = mask; nmask > 0; nmask = mask & (nmask - 1)) {
• // use nmask
•}
![Page 87: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/87.jpg)
NotesNotes
•Visits the masks in reverse order
•Does not generate the empty mask
•Visits the masks in reverse order
•Does not generate the empty mask
![Page 88: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/88.jpg)
ComplexityComplexity
•O(3n) [how?]
•Recursively running this on all subsets with memorization
•O(3n) [how?]
•Recursively running this on all subsets with memorization
![Page 89: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/89.jpg)
Use It!Use It!
•You are given n points in a co-ordinate plane.
•Find out whether you can cover these n-points with k squares (points must lie inside the squares or on the boundary) of size ‘t’ or less.
•You are given n points in a co-ordinate plane.
•Find out whether you can cover these n-points with k squares (points must lie inside the squares or on the boundary) of size ‘t’ or less.
![Page 90: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/90.jpg)
Home workHome work
•Binary search on ‘t’ can help calculate the smallest value of ‘t’ possible.
•Square Fields - Google Code Jam, Practice Contest, Problem B
•Binary search on ‘t’ can help calculate the smallest value of ‘t’ possible.
•Square Fields - Google Code Jam, Practice Contest, Problem B
![Page 91: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/91.jpg)
110110
Binary Indexed TreeBinary Indexed Tree
![Page 92: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/92.jpg)
ProblemProblem
•Design a data structure that supports the following two operations
•An array of n elements.
•Add value x at index i (1-based)
•Retrieve sum of all values from index 1 to i
•Design a data structure that supports the following two operations
•An array of n elements.
•Add value x at index i (1-based)
•Retrieve sum of all values from index 1 to i
![Page 93: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/93.jpg)
BIT!?BIT!?
•A variation on segment tree
•O(N) space
•O(log N) update
•O(log N) retrieval
•A variation on segment tree
•O(N) space
•O(log N) update
•O(log N) retrieval
![Page 94: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/94.jpg)
111111
Tower of HanoiTower of Hanoi
![Page 95: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/95.jpg)
Not this!Not this!
•A bit sequence of n-bits - from 0 to 2n-1 - encodes the disk to be moved in the i’th step
•Yes, this is uniquely determinable
•Let google show you how
•A bit sequence of n-bits - from 0 to 2n-1 - encodes the disk to be moved in the i’th step
•Yes, this is uniquely determinable
•Let google show you how
![Page 96: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/96.jpg)
But!?But!?
•Ok ok!
•Calculate the Grey Codes from 0 to 2n - 1
•n = number of disks
•The bit that changes in the i’th grey code compared to the (i-1)th grey code is the disk to move!
•0 = smallest disk
• i >= 1
•Ok ok!
•Calculate the Grey Codes from 0 to 2n - 1
•n = number of disks
•The bit that changes in the i’th grey code compared to the (i-1)th grey code is the disk to move!
•0 = smallest disk
• i >= 1
![Page 97: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/97.jpg)
Grey Codes!?Grey Codes!?
•ith Grey Code = i ^ (i >> 1)
•Consecutive grey codes differ at only 1 bit
•Sequence of combinations that differ by 1 item (selected or dropped)
•ith Grey Code = i ^ (i >> 1)
•Consecutive grey codes differ at only 1 bit
•Sequence of combinations that differ by 1 item (selected or dropped)
![Page 98: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/98.jpg)
Thats all folksThats all folks
yes, this is the end!yes, this is the end!
![Page 99: Bit Wizardry](https://reader036.fdocuments.net/reader036/viewer/2022081417/56815963550346895dc69f8f/html5/thumbnails/99.jpg)
Thank YouThank You
for being an immensely patient audiencefor being an immensely patient audience