KEMBAR78
Bit Manipulation Using C++ | PDF | Computer Programming | C++
0% found this document useful (0 votes)
27 views9 pages

Bit Manipulation Using C++

The document contains multiple C++ programs that demonstrate various bit manipulation techniques, including getting, setting, clearing, and counting bits, as well as determining odd/even status and trailing zeros. It also includes methods for finding unique numbers in arrays and calculating powers efficiently. Each program is structured with a main function that takes user input and outputs the result of the respective bit manipulation operation.

Uploaded by

DURGESH KESHRI
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)
27 views9 pages

Bit Manipulation Using C++

The document contains multiple C++ programs that demonstrate various bit manipulation techniques, including getting, setting, clearing, and counting bits, as well as determining odd/even status and trailing zeros. It also includes methods for finding unique numbers in arrays and calculating powers efficiently. Each program is structured with a main function that takes user input and outputs the result of the respective bit manipulation operation.

Uploaded by

DURGESH KESHRI
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/ 9

Get Bit

#include <iostream>

using namespace std;

bool getBit(int num, int position) {

// return (num & (1 << position)) != 0;

return ((num >> position) & 1) != 0;

int main() {

int num, position; // 0th index from end

cin >> num >> position;

cout << getBit(num, position);

return 0;

Clear Bit

#include <iostream>

using namespace std;

int clearbit(int n, int pos)

int mask = ~(1 << pos);

return (n & mask);

int main()

int n;

int pos; //0th based indexing from end

cout << "Enter number and position: ";

cin >> n >> pos;

cout << clearbit(n, pos);

return 0;

}
Set Bit

#include <iostream>

using namespace std;

int setbit(int n, int pos)

return n | (1<<pos);

int main()

{ int n;

int pos; //0th based indexing from end

cout<<"Enter number and position";

cin>>n>>pos;

cout<<setbit(n,pos);

return 0;

Count Bit

#include <iostream>

using namespace std;

int Numberofones(int n)

int count = 0;

while (n!=0)

// count+= n & 1;

if(n & 1){

count ++ ;

n = n>>1;

}
return count;

int main()

int n;

cout << "Enter the number ";

cin >> n;

cout << Numberofones(n);

return 0;

OddEven

#include <iostream>

using namespace std;

int main()

int a;

cout<<"Enter any number :";

cin>>a;

if((a&1) != 0)

cout<<"odd";

else cout<<"even";

return 0;

}
Trailing Zeros

#include <iostream>

using namespace std;

int trailZero(int n)

int zero = 0;

while (n % 2 == 0)

n = n >> 1;

zero++;

return zero;

int main()

int n;

cout << "Enter any number :";

cin >> n;

cout << endl

<<"Total zero's at last " << trailZero(n);

return 0;

Power of Two

#include <iostream>

using namespace std;

int isPowerOf2(int n)

return (n && (!(n & (n - 1))));

}
int main()

int n;

cout << "Enter the number ";

cin >> n;

cout << isPowerOf2(n);

return 0;

Update Bit ( Set Bit & Clear Bit)

#include <iostream>

using namespace std;

void updateBit(int &number, int position, bool bitValue) {

if (bitValue)

number |= (1 << position);

else

number &= ~(1 << position);

// run online

int main() {

int number, position;

bool bitValue;

cin >> number >> position >> bitValue;

if (position < 0 || position > 31) return 1;

updateBit(number, position, bitValue);

cout << number << endl;

return 0;

}
Finding Unique number in an array (where every other element appears exactly twice)

int main() {

int arr[5] = {1,2,4,2,1};

int unique = 0;

for(int i = 0; i<5; i++){

unique ^= arr[i];

cout<<unique;

Finding Two unique number in an array (where every other element appears exactly twice)

int main()

int nums[] = {1, 1, 2, 4, 4, 5};

int size = sizeof(nums) / sizeof(nums[0]);

int xorResult = 0;

for (int i = 0; i < size; ++i)

xorResult ^= nums[i];

int rightmostSetBit = xorResult & -xorResult;

int group1 = 0, group2 = 0;

for (int i = 0; i < size; ++i)

if (nums[i] & rightmostSetBit)

group1 ^= nums[i];

else

group2 ^= nums[i];

}
cout << group1 << " " << group2 << endl;

return 0;

Simplified Code for the above program

int main()

int nums[] = {1, 1, 2, 4, 4, 5};

int size = sizeof(nums) / sizeof(nums[0]);

int Xor = 0;

for (int i = 0; i < size; ++i)

Xor ^= nums[i];

int diff = Xor & (-Xor);

int x = 0;

for (int i = 0; i < size; ++i)

if (nums[i] & diff)

x ^= nums[i];

cout << x << " " << (x ^ Xor )<< endl;

return 0;

int diff = Xor & (-Xor);

// Use this to find the rightmost set bit

// You can also use XOR directly, but it does not work in all cases
Finding Unique in triplet

int main()

int arr[4] = {1, 1, 1, 3};

int res = 0;

for (int i = 0; i <= 32; i++)

int count = 0;

for (int j = 0; j < 4; j++)

if (arr[j] & 1)

count++;

arr[j] = arr[j]>>1; // right shift

if (count % 3 != 0)

res = res | (1 << i);

cout << res;

}
Fast Power

#include <iostream>

using namespace std;

int Fastpower(int a, int b)

int power = 1;

while(b>0)

if((b&1)!=0)

power = power*a;

a = a*a;

b >>=1;

return power;

int main()

int a, b;

cout<<"Enter two Numbers : ";

cin>>a>>b;

cout<<Fastpower(a,b);

return 0;

You might also like