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

7 Bit Manipulation

This document discusses bit manipulation and bitwise operations. It covers bit representation, common bitwise operators like AND, OR, XOR, and left/right shift. It also discusses applications of bitwise operations like power set representation and finding odd occurring numbers. Builtin functions for bit manipulation are also covered.

Uploaded by

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

7 Bit Manipulation

This document discusses bit manipulation and bitwise operations. It covers bit representation, common bitwise operators like AND, OR, XOR, and left/right shift. It also discusses applications of bitwise operations like power set representation and finding odd occurring numbers. Builtin functions for bit manipulation are also covered.

Uploaded by

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

BIT MANIPULATION

Programming Club, AKGEC


● All data in computer programs is internally stored as bits, i.e.,
as numbers 0 and 1.
● Bitwise operations are therefore faster than other operations
resulting in less instances of TLE.
● Some applications of bitwise :
➢ Power set representation
➢ Finding Odd occurring number.
➢ Whether a number is Power of 2
➢ Compression and Encryption
➢ And many more ..
Bit Representation
BIT NUMBER
1. Let’s assume there’s a 3 bit size datatype.
100 -4
2. 3 bits → 23 distinct numbers it can possibly
represent. 101 -3
3. Then these 23 numbers will have :
110 -2
(half -ve numbers) + (1 zero) + (rest +ve numbers)
111 -1
4. And the digits will range from -2n-1 to (2n-1 - 1), 000 0
and here n = 3.
5. For Bit with most significant bit = 1 : 001 1

Number = – (2’s compliment of Bit) 010 2

011 3
Bitwise Operators
And (&) Operator

1. All bit 1 → 1

A B A&B 2. Any bit 0 → 0


X (24) 1 1 0 0 0
0 0 0
Y (10) 0 1 0 1 0
0 1 0
Z (11) 0 1 0 1 1
1 0 0
X & Y & Z (8) 0 1 0 0 0
1 1 1
Or (|) Operator

1. All bit 0 → 0

A B A|B 2. Any bit 1 → 1


X (24) 1 1 0 0 0
0 0 0
Y (10) 0 1 0 1 0
0 1 1
Z (11) 0 1 0 1 1
1 0 1
X | Y | Z (27) 1 1 0 1 1
1 1 1
Xor (^) Operator

1. Odd 1 → 1

2. Even 1 → 0
A B A^B
X (24) 1 1 0 0 0
0 0 0
Y (10) 0 1 0 1 0
0 1 1
Z (11) 0 1 0 1 1
1 0 1
X ^ Y ^ Z (25) 1 1 0 0 1
1 1 0
Left Shift (<<) Operator

1. Shift bits to the left and append 0 at the end.


2. Equivalent to multiplying number to 2k (if we are shifting k
bits).

X (11) 0 0 0 0 1 0 1 1

X << 2 (44) 0 0 1 0 1 1 0 0
Right Shift (>>) Operator

1. Shift bits to the right and append 0 at the start.


2. Equivalent to dividing number with 2k (if we are shifting k
bits).

X (22) 0 0 0 1 0 1 1 0

X >> 3 (2) 0 0 0 0 0 0 1 0
Operations on Bit
Performing operations on a Bit at position K :

❖ ON Bit —-----> use ( OR 1 ) mask → | ( 1 <<


k)

❖ OFF Bit —-----> use ( AND 0 ) mask → & (~ ( 1


<< k ) )

❖ Toggle Bit —-----> use ( XOR 1 ) mask → ^ ( 1 <<


k)

❖ Check Bit —-----> use ( AND 1 ) mask → &(1


<< k )
Bit Magic
Kth bit is set or not ?
Given a number n and a bit position k, check whether the bit at position k is set or
not.

Example : n = 11

1 . If k = 1 → return True 2 . If k = 2 → return False

n (11) 1 0 1 1 n (11) 1 0 1 1
Implementation
Using Check Bit operation & ( 1 << K )
Power of 2 ?
Given a number n, check whether n is a power of 2.

Example : n = 8 ( 23 ) Observation :

● n & ( n - 1 ) —> will toggle Most


Significant Bit ( msb ) to Zero.
n=8 1 0 0 0
● Since, power of 2 will only have one
Set bit. Therefore on performing above
n-1=7 0 1 1 1 operation, the n should reduce to Zero.
Implementation
Toggling Most Significant Bit using n & (n - 1)
Odd Occuring Number ?
Given an array, find only number in array with odd number of occurrences.

Examples :
1. Example 1 :

Array1 3 3 2 5 5 6 6 1 1 Output : 2

2. Example
2:
Array2 4 3 4 5 5 3 4 3 3 Output : 4
Implementation
Using XOR Property ( A ^ B ) ^ B → A and A ^ 0 → A
Builtin Functions
GCC compiler provides four important builtin functions
X=5 0 0 0 0 0 1 0 1

A. __builtin_popcount ( ) :
Returns number of set bits ( number of one’s ) in an integer.

Example : int x = 5; Output : 2


cout<<__builtin_popcount(x);

B. __builtin_parity ( ) :
Odd number of set bits → returns 1
Even number of set bits → return 0

Example : int x = 5; Output : 0


cout<<__builtin_parity(x);
X=8 0 0 … 0 0 1 0 0 0

C. __builtin_clz ( ) :
Counts Leading Zeros ( clz ) in an integer.

Example : int x = 8; Output : 28


cout<<__builtin_clz(x);

D. __builtin_ctz ( ) :
Counts Trailing Zeros ( ctz ) in an integer.

Example : int x = 8; Output : 3


cout<<__builtin_ctz(x);
Thank You !

You might also like