Count number of bits changed after adding 1 to given N
Given an integer
. The task is to find the number of bits changed after adding 1 to the given number.
Examples:
Input : N = 5 Output : 2 After adding 1 to 5 it becomes 6. Binary representation of 5 is 101. Binary representation of 6 is 110. So, no. of bits changed is 2. Input : N = 1 Output : 2
There are three approaches to find the number of changed bits in the result obtained after adding 1 to the given value N:
- Approach 1: Add 1 to given integer and compare the bits of N and the result obtained after addition and count the number of unmatched bit.
- Approach 2: In case if 1 is added to N, then the total number of bits changed is defined by the position of 1st Zero from right i.e. LSB as zero. In the case, 1 is added to 1 then it got changed and pass a carry 1 to its next bit but if 1 is added to 0 only 0 changes to 1 and no further carry is passed.
- Approach 3: For finding number of changed bits when 1 is added to a given number take XOR of n and n+1 and calculate the number of set bits in the resultant XOR value.
Below is the implementation of the Approach 3:
C++
// CPP program to find the number // of changed bit #include <bits/stdc++.h> using namespace std; // Function to find number of changed bit int findChangedBit(int n) { // Calculate xor of n and n+1 int XOR = n ^ (n + 1); // Count set bits in xor value int result = __builtin_popcount(XOR); // Return the result return result; } // Driver function int main() { int n = 6; cout << findChangedBit(n) << endl; n = 7; cout << findChangedBit(n); return 0; } |
chevron_right
filter_none
Java
// Java program to find the number // of changed bit class GFG { // Function to find number of changed bit static int findChangedBit(int n) { // Calculate xor of n and n+1 int XOR = n ^ (n + 1); // Count set bits in xor value int result = Integer.bitCount(XOR); // Return the result return result; } // Driver code public static void main(String[] args) { int n = 6; System.out.println(findChangedBit(n)); n = 7; System.out.println(findChangedBit(n)); } } // This code contributed by Rajput-Ji |
chevron_right
filter_none
Python3
# Python 3 program to find the number # of changed bit # Function to find number of changed bit def findChangedBit(n): # Calculate xor of n and n+1 XOR = n ^ (n + 1) # Count set bits in xor value result = bin(XOR).count("1") # Return the result return result # Driver Code if __name__ == '__main__': n = 6 print(findChangedBit(n)) n = 7 print(findChangedBit(n)) # This code is contributed by # Surendra_Gangwar |
chevron_right
filter_none
C#
// C# program to find the number // of changed bit using System; class GFG { // Function to find number of changed bit static int findChangedBit(int n) { // Calculate xor of n and n+1 int XOR = n ^ (n + 1); // Count set bits in xor value int result = bitCount(XOR); // Return the result return result; } static int bitCount(int x) { // To store the count // of set bits int setBits = 0; while (x != 0) { x = x & (x - 1); setBits++; } return setBits; } // Driver code public static void Main(String[] args) { int n = 6; Console.WriteLine(findChangedBit(n)); n = 7; Console.WriteLine(findChangedBit(n)); } } /* This code contributed by PrinciRaj1992 */ |
chevron_right
filter_none
Output:
1 4
Recommended Posts:
- Maximum sum by adding numbers with same number of set bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count total bits in a number
- Count Set-bits of number using Recursion
- Count unset bits of a number
- Count number of set bits in a range using bitset
- Count number of bits to be flipped to convert A to B | Set-2
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count number of bits to be flipped to convert A to B
- Count pairs of elements such that number of set bits in their OR is B[i]
- Program to count number of set bits in an (big) array
- Toggle bits of a number except first and last bits
- Count set bits in an integer
- Count set bits in a range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



