Bit wizardry
This presentation is the property of its rightful owner.
Sponsored Links
1 / 100

Bit Wizardry PowerPoint PPT Presentation


  • 51 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Bit Wizardry

An Image/Link below is provided (as is) to download presentation

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


Bit wizardry

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

    Who do we think we are?


    Bit wizardry

    We

    • Dhruv Matani (tall guy)

    • Ashwin Jain (thin guy)

    • Shilp Gupta (fat guy)


    Bit wizardry

    We

    • Programmers

    • Software Developers, Directi

    • Bit Wizards


    Who do we think you are

    Who do we think you are?


    Bit wizardry

    You

    • Programmers

    • Wish to become Bit Wizards


    Bit wizards

    Bit Wizards

    • Better programmers

    • Bits are cool!


    Bit wizardry

    Why?


    Bit wizardry

    Why?

    • Bits are simple

      • zero

      • one


    Bit wizardry

    Why?

    • Bit operations are simple

      • and

      • or

      • not


    Bit wizardry

    Why?

    • 2n is common

      • Tower of Hanoi

      • Dynamic Programming

      • Set cover


    Why tower of hanoi

    Why Tower of Hanoi?

    • 3n states

    • Optimal sequence contains

      • (2n - 1) moves

    • General case?


    Why dp

    Why DP?

    • Weighted Matching

    • TSP

    • Domino Tiling

    • The list goes on..


    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.


    Why 2 n

    Why 2n?

    • n-bits to mask them all,n-bits to find them,n-bits compress them alland in an array bind them


    What the cuss was that

    What the cuss was that?


    Compress space

    Compress space

    • Premise

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

      • byte = 8 bits

    • Conclusion

      • Waste not, want not


    Faster lookup

    Faster lookup?

    • Masks are numbers

    • Numbers make lightening fast indexes

    • Think, array!


    Are you convinced

    Are you convinced?

    • Say yes.


    Pay attention

    Pay attention!

    • You too @ashwin + @dhruv!


    Rules

    Rules

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


    Caution

    Caution

    • Parenthesize your bit operations

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


    Bit wizardry

    001

    • Bit by Bit


    Bit wizardry

    bit

    • A unit of information.

      • 0 or 1


    Bit wizardry

    byte

    • 1 byte = 8 bits

    • char (g++)


    Short int

    short int

    • 1 short int = 16 bits

    • short int (g++)

    • int (turbo c++)


    Bit wizardry

    int

    • 1 int = 4 bytes = 32 bits

    • int (g++)

    • long int (turbo c++)


    Long long int

    long long int

    • 1 long long int = 64 bits

    • long long int (g++)

    • __int64 (visual c++)


    Bit wizardry

    and

    • conjunction

    • truth table


    Bit wizardry

    or

    • disjunction

    • truth table


    Bit wizardry

    not

    • negation

    • truth table


    Bit wizardry

    xor

    • exclusive or / toggle

    • truth table


    Bitwise operators

    bitwise operators

    • x & y

    • x | y

    • ~x

    • x^y


    Shift operators

    shift operators

    • >>

    • <<


    Do it yourself

    Do it yourself.

    • wake up, open your computer, try them


    Print the input in binary using bitwise operators

    Print the input in binary using bitwise operators.


    Sieve the primes 1024 using an array of 32 ints only

    Sieve the primes < 1024 using an array of 32 ints only!


    Solution

    Solution

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

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


    Bit wizardry

    010

    • Things you should know


    Bit wizardry

    2n

    • 1 followed by n 0’s


    2 n 1

    2n - 1

    • 1 repeated n times


    Bit wizardry

    -1

    • all 1’s


    Bit wizardry

    <<

    • multiply by power of 2


    Bit wizardry

    >>

    • divide by power of 2


    2 n 11

    & (2n - 1)

    • remainder on division by 2n


    Bit wizardry

    x & 1

    • is the number even or odd?


    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?


    X x 11

    x & (x + 1)

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


    Bit wizardry

    x & (-x)

    • smallest power of 2 in the binary expansion of x


    Bit wizardry

    x & (-x)

    • -x = ~x + 1

    • 2’s complement notation

    • Understand this very carefully!


    X x 12

    ~x & (x + 1)

    • isolate the rightmost 0


    Swap x y

    swap(x,y)

    • x ^= y ^= x ^= y


    Do it yourself1

    Do it yourself

    • do you have your pens and pencils?


    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.


    Bit wizardry

    011

    • Binary searching on bits


    Bit wizardry

    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)


    Bit wizardry

    Huh!?

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

      • O(log log n) !?


    Definitions

    Definitions

    • Given x. (Assume it has 32 bits)


    Step 1

    Step 1

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


    Step 2

    Step 2

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


    Step 3

    Step 3

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


    Step 4

    Step 4

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


    Step 5

    Step 5

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


    Bit wizardry

    End

    • return x


    Beyond the horizon

    Beyond the horizon

    • Think of the several possibilities

      • Compute the parity of the number of 1’s

      • Reverse the bits of a number


    Do it yourself2

    Do it yourself

    • the s**t has hit the fan!


    Given x find the highest power of 2 less than x

    Given x, find the highest power of 2, less than x


    Solution1

    Solution

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

    • return x - (x >> 1)


    Bit wizardry

    100

    • Invention of masking


    The idea

    The Idea

    • You know set

    • You know subset

    • Lets number subsets


    The idea1

    The 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


    The idea2

    The Idea

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


    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!


    Realize

    Realize

    • | = set union

    • & = set intersection

    • -1 ^ A = set negation


    Think

    Think!

    • Set subtraction

    • Adding

    • Deleting

    • Testing set participation


    Dynamic programming

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


    Concentrate

    Concentrate

    • a presentation can only say as much!


    Understand

    Understand

    • A matrix of numbers

    • Selection of items

      • exactly 1 in each row

      • exactly 1 in each column


    A brute force approach

    A brute force approach

    • n!

    • Consider all permutations


    A faster brute force

    A faster brute force

    • O(n 2n)

    • Each mask selects a subset of columns

      • And as many rows as columns (from top)


    Lets chalk it

    Lets chalk it


    Home work

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


    Home work1

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


    Bit wizardry

    101

    • Set cover


    The problem

    The Problem

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


    The solution

    The Solution

    • for(nmask = mask; nmask > 0; nmask = mask & (nmask - 1)) {

    • // use nmask

    • }


    Notes

    Notes

    • Visits the masks in reverse order

    • Does not generate the empty mask


    Complexity

    Complexity

    • O(3n) [how?]

    • Recursively running this on all subsets with memorization


    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.


    Home work2

    Home work

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

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


    Bit wizardry

    110

    • Binary Indexed Tree


    Problem

    Problem

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

    BIT!?

    • A variation on segment tree

      • O(N) space

      • O(log N) update

      • O(log N) retrieval


    Bit wizardry

    111

    • Tower of Hanoi


    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


    Bit wizardry

    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


    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)


    Thats all folks

    Thats all folks

    • yes, this is the end!


    Thank you

    Thank You

    • for being an immensely patient audience


    Blame praise us @

    Blame / Praise us @

    • [email protected]

    • [email protected]

    • [email protected]


  • Login