Increment a number by one by manipulating the bits

15 Jun, 2022
Given a non-negative integer n. The problem is to increment n by 1 by manipulating the bits of n.Examples :  
Input : 6
Output : 7

Input : 15
Output : 16
 
Approach: Following are the steps: 
Get the position of rightmost unset bit of n. Let this position be k.Set the k-th bit of n.Toggle the last k-1 bits of n.Finally, return n. 
Get the position of rightmost unset bit of n. Let this position be k.
Set the k-th bit of n.
Toggle the last k-1 bits of n.
Finally, return n. 
 
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation to increment a number// by one by manipulating the bits#include <bits/stdc++.h>using namespace std; // function to find the position// of rightmost set bitint getPosOfRightmostSetBit(int n){    return log2(n & -n);} // function to toggle the last m bitsunsigned int toggleLastKBits(unsigned int n,                             unsigned int k){    // calculating a number 'num' having 'm' bits    // and all are set    unsigned int num = (1 << k) - 1;     // toggle the last m bits and return the number    return (n ^ num);} // function to increment a number by one// by manipulating the bitsunsigned int incrementByOne(unsigned int n){    // get position of rightmost unset bit    // if all bits of 'n' are set, then the    // bit left to the MSB is the rightmost    // unset bit    int k = getPosOfRightmostSetBit(~n);     // kth bit of n is being set by this operation    n = ((1 << k) | n);     // from the right toggle all the bits before the    // k-th bit    if (k != 0)        n = toggleLastKBits(n, k);     // required number    return n;} // Driver program to test aboveint main(){    unsigned int n = 15;    cout << incrementByOne(n);    return 0;}
// Java implementation to increment a number// by one by manipulating the bitsimport java.io.*;import java.util.*; class GFG {         // function to find the position    // of rightmost set bit    static int getPosOfRightmostSetBit(int n)    {        return (int)(Math.log(n & -n) / Math.log(2));    }         // function to toggle the last m bits    static int toggleLastKBits( int n, int k)    {        // calculating a number 'num' having        // 'm' bits and all are set        int num = (1 << k) - 1;             // toggle the last m bits and return        // the number        return (n ^ num);    }         // function to increment a number by one    // by manipulating the bits    static int incrementByOne( int n)    {        // get position of rightmost unset bit        // if all bits of 'n' are set, then the        // bit left to the MSB is the rightmost        // unset bit        int k = getPosOfRightmostSetBit(~n);             // kth bit of n is being set        // by this operation        n = ((1 << k) | n);             // from the right toggle all        // the bits before the k-th bit        if (k != 0)            n = toggleLastKBits(n, k);             // required number        return n;         }         // Driver Program       public static void main (String[] args)    {        int n = 15;        System.out.println(incrementByOne(n));             }} // This code is contributed by Gitanjali.
# python 3 implementation# to increment a number# by one by manipulating# the bitsimport math # function to find the# position of rightmost# set bitdef getPosOfRightmostSetBit(n) :    return math.log2(n & -n)      # function to toggle the last m bitsdef toggleLastKBits(n, k) :    # calculating a number     # 'num' having 'm' bits    # and all are set    num = (1 << (int)(k)) - 1     # toggle the last m bits and    # return the number    return (n ^ num)  # function to increment# a number by one by# manipulating the bitsdef incrementByOne(n) :     # get position of rightmost    # unset bit if all bits of    # 'n' are set, then the bit    # left to the MSB is the    # rightmost unset bit    k = getPosOfRightmostSetBit(~n)     # kth bit of n is being    # set by this operation    n = ((1 << (int)(k)) | n)     # from the right toggle    # all the bits before the    # k-th bit    if (k != 0) :        n = toggleLastKBits(n, k)     # required number    return n       # Driver programn = 15print(incrementByOne(n))  # This code is contributed# by Nikita Tiwari.
// C# implementation to increment a number// by one by manipulating the bitsusing System; class GFG {         // function to find the position    // of rightmost set bit    static int getPosOfRightmostSetBit(int n)    {        return (int)(Math.Log(n & -n) / Math.Log(2));    }         // function to toggle the last m bits    static int toggleLastKBits( int n, int k)    {                 // calculating a number 'num' having        // 'm' bits and all are set        int num = (1 << k) - 1;             // toggle the last m bits and return        // the number        return (n ^ num);    }         // function to increment a number by one    // by manipulating the bits    static int incrementByOne( int n)    {                 // get position of rightmost unset bit        // if all bits of 'n' are set, then the        // bit left to the MSB is the rightmost        // unset bit        int k = getPosOfRightmostSetBit(~n);             // kth bit of n is being set        // by this operation        n = ((1 << k) | n);             // from the right toggle all        // the bits before the k-th bit        if (k != 0)            n = toggleLastKBits(n, k);             // required number        return n;         }         // Driver Program    public static void Main ()    {        int n = 15;                 Console.WriteLine(incrementByOne(n));             }} // This code is contributed by Sam007.
<?php// PHP implementation to increment a number// by one by manipulating the bits // function to find the position// of rightmost set bitfunction getPosOfRightmostSetBit($n){    $t = $n & -$n;    return log($t, 2);} // function to toggle the last m bitsfunction toggleLastKBits($n, $k){    // calculating a number 'num'    // having 'm' bits and all are set    $num = (1 << $k) - 1;     // toggle the last m bits and    // return the number    return ($n ^ $num);} // function to increment a number by one// by manipulating the bitsfunction incrementByOne($n){    // get position of rightmost unset bit    // if all bits of 'n' are set, then the    // bit left to the MSB is the rightmost    // unset bit    $k = getPosOfRightmostSetBit(~$n);     // kth bit of n is being set    // by this operation    $n = ((1 << $k) | $n);     // from the right toggle all the    // bits before the k-th    if ($k != 0)        $n = toggleLastKBits($n, $k);     // required number    return $n;} // Driver code$n = 15;echo incrementByOne($n); // This code is contributed by Mithun Kumar?>
<script> // JavaScript program to increment a number// by one by manipulating the bits     // function to find the position    // of rightmost set bit    function getPosOfRightmostSetBit(n)    {        return (Math.log(n & -n) / Math.log(2));    }           // function to toggle the last m bits    function toggleLastKBits(n, k)    {        // calculating a number 'num' having        // 'm' bits and all are set        let num = (1 << k) - 1;               // toggle the last m bits and return        // the number        return (n ^ num);    }           // function to increment a number by one    // by manipulating the bits    function incrementByOne(n)    {        // get position of rightmost unset bit        // if all bits of 'n' are set, then the        // bit left to the MSB is the rightmost        // unset bit        let k = getPosOfRightmostSetBit(~n);               // kth bit of n is being set        // by this operation        n = ((1 << k) | n);               // from the right toggle all        // the bits before the k-th bit        if (k != 0)            n = toggleLastKBits(n, k);               // required number        return n;           }   // Driver code                 let n = 15;        document.write(incrementByOne(n));         </script>
Output : 
16
Time Complexity: O(1)Space Complexity: O(1) 
Mithun Kumar
souravghosh0416
ranjanrohit840
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find whether a given number is power of 2
Bits manipulation (Important tactics)
Little and Big Endian Mystery
Binary representation of a given number
Divide two integers without using multiplication, division and mod operator
Josephus problem | Set 1 (A O(n) Solution)
Bit Fields in C
Find the element that appears once
C++ bitset and its application
Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2