Toggle last set bit in an integer

If N is the given integer, we toggle the last set bit in N by doing N = N - ( N & ( -N ))

Toggle_Last_Set_Bit



Program to toggle the last set bit in an integer.

# Toggle the last set bit in the given integer

def Toggle_Last_Set_Bit (n : int) -> int :
       
     # Example n = 5 ( 1 0 1 )
     # -n is 2's complement of n
     # -n = 1 0 1 >> 0 1 0 (1's complement) 
     #             + 0 0 1 (+ 1)
     #              -------
     #               0 1 1
     # n & ( -n ) =  1 0 1 
     #             & 0 1 1
     #              -------
     #               0 0 1 
     # n - ( n & ( -n ) ) = 1 0 1 - 0 0 1 = ( 5 - 1 ) = 4 i.e ( 1 0 0 )
     return n - (n & (-n))

def main() :
     
    n = int(input("Enter number : "))
    print("Number after toggling the last set bit : " + str(Toggle_Last_Set_Bit(n)))

if __name__ == "__main__" :
    main()

Output

Enter number : 10
Number after toggling the last set bit : 8

Enter number : 12
Number after toggling the last set bit : 8

Enter number : 256
Number after toggling the last set bit : 0
#include<iostream>

using namespace std;

// Toggle the last set bit in the given integer
void Toggle_Last_Set_Bit (int& n) {
     /* 
      Example n = 4 ( 1 0 0 )
     -n is 2's complement of n
     -n = 1 0 0 >> 0 1 1 ( 1's complement )
                 + 0 0 1 ( Add 1 )
                 -------
                   1 0 0
      n & ( -n ) = 1 0 0
                 & 1 0 0
                 -------
                   1 0 0
      n - ( n & ( -n ) ) = 1 0 0 - 1 0 0 = ( 4 - 4 ) = 0 */
     n = n - (n & (-n));
}

int main() {
     
    int n;
    cout << "Enter number : ";
    cin >> n;
    Toggle_Last_Set_Bit (n);
    cout << "Number after toggling the last set bit : " << n << endl;
    return 0;
}

Output

Enter number : 10
Number after toggling the last set bit : 8

Enter number : 6
Number after toggling the last set bit : 4

Enter number : 4
Number after toggling the last set bit : 0

Enter number : 3
Number after toggling the last set bit : 2

Enter number : 16
Number after toggling the last set bit : 0
import java.util.Scanner;

class Bitwise {

    // Toggle the last set bit in the given integer
    static int Toggle_Last_Set_Bit (int n) {
        /* 
        Example n = 10 ( 1 0 1 0 )
        -n is 2's complement of n
        -n = 1 0 1 0 >> 0 1 0 1  (1's complement)
                     +  0 0 0 1  ( Add 1 )
                        -------
                        0 1 1 0

         n & ( -n ) = 1 0 1 0
                    & 0 1 1 0
                      -------
                      0 0 1 0
         n - ( n & ( -n ) ) = 1 0 1 0 - 0 0 1 0 = ( 10 - 2 ) = 8 ( 1 0 0 0 ) */

        return n - (n & (-n));
    }

    public static void main (String[] args) {

        System.out.print("Enter number : ");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println( "Number after toggling the last set bit : " + Toggle_Last_Set_Bit (n));
    }
}

Output

Enter number : 10
Number after toggling the last set bit : 8

Enter number : 4
Number after toggling the last set bit : 0
Enter number : 16

Number after toggling the last set bit : 0

Enter number : 7
Number after toggling the last set bit : 6



Copyright (c) 2019-2021, Algotree.org.
All rights reserved.