Toggle first and last bits of a number
Given a number n, the task is to toggle only first and last bits of a number
Examples :
Input : 10 Output : 3 Binary representation of 10 is 1010. After toggling first and last bits, we get 0011. Input : 15 Output : 6
Prerequisite : Find MSB of given number.
1) Generate a number which contains first and last bit as set. We need to change all middle bits to 0 and keep corner bits as 1.
2) Answer is XOR of generated number and original number.
C++
// CPP program to toggle first and last // bits of a number #include <iostream> using namespace std; // Returns a number which has same bit // count as n and has only first and last // bits as set. int takeLandFsetbits(int n) { // set all the bit of the number n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; // Adding one to n now unsets // all bits and moves MSB to // one place. Now we shift // the number by 1 and add 1. return ((n + 1) >> 1) + 1; } int toggleFandLbits(int n) { // if number is 1 if (n == 1) return 0; // take XOR with first and // last set bit number return n ^ takeLandFsetbits(n); } // Driver code int main() { int n = 10; cout << toggleFandLbits(n); return 0; } |
chevron_right
filter_none
Java
// Java program to toggle first and last // bits of a number import java.io.*; class GFG { // Returns a number which has same bit // count as n and has only first and last // bits as set. static int takeLandFsetbits(int n) { // set all the bit of the number n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; // Adding one to n now unsets // all bits and moves MSB to // one place. Now we shift // the number by 1 and add 1. return ((n + 1) >> 1) + 1; } static int toggleFandLbits(int n) { // if number is 1 if (n == 1) return 0; // take XOR with first and // last set bit number return n ^ takeLandFsetbits(n); } // Driver code public static void main(String args[]) { int n = 10; System.out.println(toggleFandLbits(n)); } } /*This code is contributed by Nikita Tiwari.*/ |
chevron_right
filter_none
Python3
# Python 3 program to toggle first # and last bits of a number. # Returns a number which has same bit # count as n and has only first and last # bits as set. def takeLandFsetbits(n) : # set all the bit of the number n = n | n >> 1 n = n | n >> 2 n = n | n >> 4 n = n | n >> 8 n = n | n >> 16 # Adding one to n now unsets # all bits and moves MSB to # one place. Now we shift # the number by 1 and add 1. return ((n + 1) >> 1) + 1 def toggleFandLbits(n) : # if number is 1 if (n == 1) : return 0 # take XOR with first and # last set bit number return n ^ takeLandFsetbits(n) # Driver code n = 10print(toggleFandLbits(n)) # This code is contributed by Nikita Tiwari. |
chevron_right
filter_none
C#
// C# program to toggle first and last // bits of a number using System; class GFG { // Returns a number which has same bit // count as n and has only first and last // bits as set. static int takeLandFsetbits(int n) { // set all the bit of the number n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; // Adding one to n now unsets // all bits and moves MSB to // one place. Now we shift // the number by 1 and add 1. return ((n + 1) >> 1) + 1; } static int toggleFandLbits(int n) { // if number is 1 if (n == 1) return 0; // take XOR with first and // last set bit number return n ^ takeLandFsetbits(n); } // Driver code public static void Main() { int n = 10; Console.WriteLine(toggleFandLbits(n)); } } // This code is contributed by Anant Agarwal. |
chevron_right
filter_none
PHP
<?php // PHP program to toggle first and last // bits of a number // Returns a number which has same bit // count as n and has only first and last // bits as set. function takeLandFsetbits($n) { // set all the bit of the number $n |= $n >> 1; $n |= $n >> 2; $n |= $n >> 4; $n |= $n >> 8; $n |= $n >> 16; // Adding one to n now unsets // all bits and moves MSB to // one place. Now we shift // the number by 1 and add 1. return (($n + 1) >> 1) + 1; } function toggleFandLbits(int $n) { // if number is 1 if ($n == 1) return 0; // take XOR with first and // last set bit number return $n ^ takeLandFsetbits($n); } // Driver code $n = 10; echo toggleFandLbits($n); // This code is contributed by mits ?> |
chevron_right
filter_none
Output :
3
Time Complexity is O(1).
Recommended Posts:
- Toggle bits of a number except first and last bits
- For every set bit of a number toggle bits of other
- Toggle all the bits of a number except k-th bit.
- Toggle all even bits of a number
- Toggle all odd bits of a number
- Toggle the last m bits
- Toggle all bits after most significant bit
- Toggle bits in the given range
- Check if bits of a number has count of consecutive set bits in increasing order
- Program to toggle K-th bit of a number N
- Set, Clear and Toggle a given bit of a number in C
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Check if a number has same number of set and unset bits
- Set all even bits of a number
- Set all odd bits of a number
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.
Improved By : Mithun Kumar


