1 / 100

Bit Wizardry - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

PowerPoint Slideshow about ' Bit Wizardry' - aglaia

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

• 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

• Dhruv Matani (tall guy)

• Ashwin Jain (thin guy)

• Shilp Gupta (fat guy)

• Programmers

• Software Developers, Directi

• Bit Wizards

• Programmers

• Wish to become Bit Wizards

• Better programmers

• Bits are cool!

• Bits are simple

• zero

• one

• Bit operations are simple

• and

• or

• not

• 2n is common

• Tower of Hanoi

• Dynamic Programming

• Set cover

• 3n states

• Optimal sequence contains

• (2n - 1) moves

• General case?

• Weighted Matching

• TSP

• Domino Tiling

• The list goes on..

• 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

• Premise

• byte - smallest unit of “point-able” memory

• byte = 8 bits

• Conclusion

• Waste not, want not

• Numbers make lightening fast indexes

• Think, array!

• Say yes.

• You too @ashwin + @dhruv!

• If in doubt / distress / disbelief / discomfort / disorientation or difficulty, ask question

• If using Turbo C++ / Borland C++, use long int instead of int

• Bit by Bit

• A unit of information.

• 0 or 1

• 1 byte = 8 bits

• char (g++)

• 1 short int = 16 bits

• short int (g++)

• int (turbo c++)

• 1 int = 4 bytes = 32 bits

• int (g++)

• long int (turbo c++)

• 1 long long int = 64 bits

• long long int (g++)

• __int64 (visual c++)

• conjunction

• truth table

• disjunction

• truth table

• negation

• truth table

• exclusive or / toggle

• truth table

• x & y

• x | y

• ~x

• x^y

• >>

• <<

• wake up, open your computer, try them

• SET(n) A[n >> 5] |= (1 << (n & 31))

• UNSET(n) A[n>>5] &= ~(1 << (n&31))

• Things you should know

2n

• 1 followed by n 0’s

2n - 1

• 1 repeated n times

• all 1’s

• multiply by power of 2

• divide by power of 2

& (2n - 1)

• remainder on division by 2n

• is the number even or odd?

• is x a power of 2?

• x &= (x-1) removes the least significant set bit!

• can you think of immediate uses?

• is the binary expansion of x all 1’s?

• smallest power of 2 in the binary expansion of x

• -x = ~x + 1

• 2’s complement notation

• Understand this very carefully!

• isolate the rightmost 0

• x ^= y ^= x ^= y

• do you have your pens and pencils?

011 number of set bits.

• Binary searching on bits

BS!? number of set 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)

Huh!? number of set bits.

• Lets do it in O(log of the number of bits).

• O(log log n) !?

Definitions number of set bits.

• Given x. (Assume it has 32 bits)

Step 1 number of set bits.

• X = (X & 0x55555555) + ((x & 0xAAAAAAAA) >> 1)

Step 2 number of set bits.

• X = (X & 0x33333333) + ((x & 0xcccccccc) >> 2)

Step 3 number of set bits.

• X = (X & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4)

Step 4 number of set bits.

• X = (X & 0x00ff00ff) + ((x & 0xff00ff00) >> 8)

Step 5 number of set bits.

• X = (X & 0x0000ffff) + ((x & 0xffff0000) >> 16)

End number of set bits.

• return x

Beyond the horizon number of set bits.

• Think of the several possibilities

• Compute the parity of the number of 1’s

• Reverse the bits of a number

Do it yourself number of set bits.

• the s**t has hit the fan!

Solution number of set bits.

• x |= x >> 1x |= x >> 2x |= x >> 4x |= x >> 8x |= x >> 16

• return x - (x >> 1)

100 number of set bits.

The Idea number of set bits.

• You know set

• You know subset

• Lets number subsets

The Idea number of set bits.

• label items of a set from {0, 1, 2, 3 ... (n-1)}

• n items = 2n subsets

• lets number subsets from 0 to 2n - 1

The Idea number of set bits.

• Subset number ‘x’ contains item numbered ‘i’ iff bit ‘i’ is set in ‘x’, and vice versa

Get it? number of set bits.

• x is the mask of the subset

• directly maps to a subset

• an iteration from 0 to 2n - 1 iterates over all subsets!

Realize number of set bits.

• | = set union

• & = set intersection

• -1 ^ A = set negation

Think! number of set bits.

• Set subtraction

• Deleting

• Testing set participation

Dynamic Programming number of set bits.

• 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!

Concentrate number of set bits.

• a presentation can only say as much!

Understand number of set bits.

• A matrix of numbers

• Selection of items

• exactly 1 in each row

• exactly 1 in each column

A brute force approach number of set bits.

• n!

• Consider all permutations

A faster brute force number of set bits.

• O(n 2n)

• Each mask selects a subset of columns

• And as many rows as columns (from top)

Lets chalk it number of set bits.

Home work number of set bits.

• 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!

Home work number of set bits.

• 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!

101 number of set bits.

• Set cover

The Problem number of set bits.

• Given a mask, how do you calculate the masks that depict the subsets of this mask.

The Solution number of set bits.

• }

Notes number of set bits.

• Visits the masks in reverse order

• Does not generate the empty mask

Complexity number of set bits.

• O(3n) [how?]

• Recursively running this on all subsets with memorization

Use It! number of set bits.

• 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.

Home work number of set bits.

• Binary search on ‘t’ can help calculate the smallest value of ‘t’ possible.

• Square Fields - Google Code Jam, Practice Contest, Problem B

110 number of set bits.

• Binary Indexed Tree

Problem number of set bits.

• 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

BIT!? number of set bits.

• A variation on segment tree

• O(N) space

• O(log N) update

• O(log N) retrieval

111 number of set bits.

• Tower of Hanoi

Not this! number of set bits.

• 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

But!? number of set bits.

• 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

Grey Codes!? number of set bits.

• 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)

Thats all folks number of set bits.

• yes, this is the end!

Thank You number of set bits.

• for being an immensely patient audience

Blame / Praise us @ number of set bits.