BITWISE OPERATION & BIT MANIPULATION_Abhijeet.pptx

rohitcocrider1234 8 views 20 slides Mar 06, 2025
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

bit manipualtion


Slide Content

BITWISE OPERATION & BIT MANIPULATION Bit manipulation is a technique in competitive programming that involves the manipulation of individual bits in binary representations of numbers. WHY BITWISE ? In CPU information is stored in bits and ALU perform arithmetic operations in bit level Allows to solve problems efficiently Reducing time complexity and memory usage

BINARY NUMBER SYSTEM The binary number system is a mathematical expression that uses only two symbols: 0 and 1. DECIMAL-> 10 base system (0 -9) BINARY → 2 base system ( 0-1) 1 - set bit ; 0- unset bit

DECIMAL-BINARY CONVERSION 17=16+1 =1*(16)+0*(8)+0*(4)+1*(1) =1*( )+0( )+0( )+0( )+1( ) = 10001  

CODE FOR DECIMAL TO BINARY CONVERSION # include < iostream > using namespace std ; long long decimalToBinary ( int n ){ long long ans = ; int remainder , i = 1 ; // Until the value of n becomes 0. while ( n != ){ remainder = n % 2 ; ans += remainder * i ; i = i * 10 ; n = n / 2 ; } return ans ; } int main () { int n = 156 ; long long binary = decimalToBinary ( n ); cout << n << " in Decimal is " << binary << endl ; return ;}

BITWISE OPERATORS BINARY AND (&) BINARY OR ( | ) BINARY NOT (!) BINARY XOR (^) LEFT SHIFT (<<) RIGHT SHIFT (>>)

BITWISE AND (&) EXAMPLE: int x = 11, y = 5, z=x&y ; z=1 ;

BITWISE OR ( | ) EXAMPLE: int x = 11, y = 7 z=x | y; z=15 ;

BITWISE NOT ( ! ) EXAMPLE: N = (101) !N = (010) A !A 1 1

BITWISE XOR ( ^ ) EXAMPLE: int x = 11, y = 7 z= x^y ; Z = 12 ;

Imp. properties of XOR A ^ A = 0; A ^ 0 = A; XOR is distributive. A ^ B ^ C = A ^ C ^ B ;

BITWISE RIGHT SHIFT ( >> ) N >> d shifts all the bits in N to right by d places 0s are added on left to fill in. 7>> 2 = ( 111 ) >> 1 = ( 011 ) = 3 4. N >> d is essentially equivalent to rounded down  

BITWISE LEFT SHIFT ( << ) N << d shifts all the bits in N to left by d places 0s are added on right to fill in. 7<< 2 = ( 111 ) >> 1 = ( 1110 ) = 14 4. N >> d is essentially equivalent to N *  

Some BITWISE tricks Set a Bit of a number Unset a bit of number Flip a bit

Set a bit of a number A : 10 01 or 00 1 00 10 1 01 Code: n = n | (1 << pos); Take bitwise or at position bit

UnSet a bit of a number A : 10 1 01 & 11 11 10 01 CODE: n &= (~(1 << pos));

Flip a bit A : 10 1 01 & 00 1 00 10 01 CODE: n ^= (1 << pos);

Q.Count no of set bits in integer 1.Simple Method : Loop through all bits in an integer, check if a bit is set and if it is, then increment the set bit count. 2.Inbuilt function : In C++, __builtin_popcount(x) returns popcount of a number — the number of ones in the binary representation of x. Use __builtin_popcountll(x) for long longs

Q.Find unique number in array of integers Input: nums = [4,1,2,1,2] Output: 4 Input: nums = [2,2,1] Output: 1

1.Simple Method : Loop through all elements and , check if there is repetition of that element. 2.XOR: Simply take XOR of all elements. CODE: for(int i =1;i<n;i++){ ans^=nums[i]; } return ans ;

FURTHER RESOURCES Bitwise operations for beginners[CODEFORCES] CP-algorithm BIT MASKING [LECTURE] Subset generation lecture SUBSET GENERATION Q.
Tags