0% found this document useful (0 votes)
8 views23 pages

Bit Manipulation 2

The document discusses techniques for bit manipulation, including cool tricks and the use of bitmasks and bitsets in programming. It explains how bitmasks can save memory compared to boolean arrays and introduces C++ STL's bitset for handling larger sets of bits efficiently. Additionally, it categorizes problems into bit independent and bit dependent, providing examples for each type.

Uploaded by

Devansh Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views23 pages

Bit Manipulation 2

The document discusses techniques for bit manipulation, including cool tricks and the use of bitmasks and bitsets in programming. It explains how bitmasks can save memory compared to boolean arrays and introduces C++ STL's bitset for handling larger sets of bits efficiently. Additionally, it categorizes problems into bit independent and bit dependent, providing examples for each type.

Uploaded by

Devansh Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

BIT MANIPULATION 2

By Harsh Gupta
Goal
• Cool tips for bit manipulation.
• Brute-forcing using bitmasks.
• Understanding bitsets.
Some Cool Tricks
● Swap 2 numbers without a temporary variable
● 2^k - 1 has last k bits set
● builtin_popcount and builtin_popcountll
● builtin_clz / _ builtin_clzll
● builtin_ctz / _builtin_ctzll
Bitmasking
A bitmask is nothing but a sequence of bits where every bit represents
something.

Think of this as a boolean array which takes less memory.

Memory consumed by a boolean array of size 30 = 30 bytes = 240 bits


Here we are using 1 byte for storing true/false which could have been
represented by a single bit.

Can you think of some better way which consumes less memory?
Instead of using an array of 30 boolean variables
We can use a sequence of 30 bits where

ith bit will be set if a[i] was true

else ith bit will be unset


We can use a 32-bit integer for allocating this memory so total
memory used now is just 32 bits.
Bitmasking – Brute Force Code

for (int mask = 0; mask < (1 << n); mask++) {


for (int i = 0; i < n;i++)
if ((mask & (1<<i))
cout << a[i] << " ";
}
cout << endl;
}
THINK

But what if the array size was 100?

We cannot create a single variable with that many bits.

But we can create, 4 32-bit integers or 2 64-bit integers.

Looks like it is becoming complicated.

If we required 320 bits then it might become complicated for us.


Bitsets
In C++ STL, we have a data structure for this - Bitsets
Syntax:-

Defining bitset:

bitset<320> bit;

Accessing and setting ith bit:

bit[i] can be used to access and set ith bit just like we use arr[i] to
access or set the ith element of an array
Bitsets (CONTD…)

● Binary Operations can be performed on Bitsets


● We can initialize bitsets with some binary string or integer also.

Some Useful Functions:-


● bit.count() -> returns the number of set bits
● bit.flip() -> flips all the bits
● any(), none() and all()
● to_string() -> returns binary string
Bit Independent Problems

In these problems, whether the current bit is set or not doesn’t affect
the other bits, so we can just solve for every bit individually.

Problem:

● XORwice
Bit Dependent Problems
Here the bits are interdependent, whether we set a bit or unset it can
affect other bits.

Example - Find the largest number less than 2^8 where at most 3 bits
are set.

Here the whether we can set a bit or not depends upon the other bits
as we cannot set more than 3 bits.

Problem:

● Gardener and the Array

You might also like