
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
For every set bit of a number toggle bits of other in C++
In this problem, we are given two integer values. Our task is to create a c program to operform the operation, For every set bit of a number toggle bits of other.
Let’s take an example to understand the problem
Input: 3 7 Output: 4 Binary of 3: 011 Binary of 3: 111
First and second bits of the second number is flipped which makes it 100 i.e 4.
Solution Approach
An approach to solve the problem is by performing the XOR operation of the two numbers. The result will be toggled for the bit where ever the bits of the I’st is 1 using XOR operation.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; int main(){ int a = 3, b = 7; cout<<"The numbers are "<<a<<" & "<<b<<endl; cout<<"The result of flipping bits is "<<(a ^ b); return 0; }
Output
The numbers are 3 & 7 The result of flipping bits is 4
- Related Articles
- How do you set, clear, and toggle a bit in C/C++?
- Minimum number using set bits of a given number in C++
- Number of integers with odd number of set bits in C++
- Clear/Set a specific bit of a number in Arduino
- Check if bits of a number has count of consecutive set bits in increasing order in Python
- Find most significant set bit of a number in C++
- Check if all bits of a number are set in Python
- Find all combinations of k-bit numbers with n bits set where 1
- Next higher number with same number of set bits in C++
- Queries for number of array elements in a range with Kth Bit Set using C++
- Prime Number of Set Bits in Binary Representation in C++
- Prime Number of Set Bits in Binary Representation in Python
- Check if a number has same number of set and unset bits in C++
- Set a bit for BigInteger in Java
- Position of the K-th set bit in a number in C++

Advertisements