Toggle First and Last Bits of a Number


The following article provides an in depth explanation of the method used to modify a number by toggling its first and last bit using bitwise operators. A bitwise operator is an operator that can be used to manipulate individual bits in binary numbers or bit patterns.

Problem Statement

For a given number n, modify the number such that the first and the last bit of the binary expansion of the new number are flipped i.e. if the original bit is 1 then the flipped bit should be 0 and vice versa. All the bits between the first and the last bit should be left unchanged.

Examples

Input: 13
Output: 4

Explanation

The binary expansion of 13 is 1101.

On toggling the first and the last bit, the expansion becomes 0100 which is equal to 4.

Hence the output is 4.

Input: 27
Output: 10

Explanation

The binary expansion of 27 is 11011.

On toggling the first and the last bit, the expansion becomes 01010 which is equal to 10.

Hence the output is 10.

Input: 113
Output: 48

Explanation

The binary expansion of 113 is 1110001.

On toggling the first and the last bit, the expansion becomes 0110000 which is equal to 48.

Hence the output is 48.

Solution Approach

This approach makes use of the bitwise XOR and left shift operator. If the corresponding bit of both operands is different, the bitwise XOR operator evaluates to 1; otherwise, it evaluates to 0. We'll employ the bitwise XOR operator's ability to toggle a bit. For instance, if the first bit of the number, n, is 1, then n ^ 1 will cause the number's first bit to be 0. In addition, if the number's first bit is set to 0, the operation n ^ 1 will change it to 1.

To flip the first bit of the number, n, we compute n ^ 1. It performs a XOR operation between the Least Significant Bit or the first bit of n with 1 to invert its value.

Inorder to flip the last bit we generate a number, k, in which only the last bit is set. The position of the last bit , r, is equal to log2(n). This is because log2(n) bits are used in the binary expansion of n.

The following steps are performed to implement this approach −

  • If n = 1, display 0 and return.

  • Toggle the first bit of the number by taking an XOR of the n with 1.

  • Toggle the last bit of the number by taking an XOR of the n with 1 << k, where k is the log2(n)th bit of the number.

  • Display the answer.

Dry Run

Let us first understand the working of the bitwise XOR(^) operator.

Input Flag Input ^ Flag
0 0 0
0 1 1
1 0 1
1 1 0

It can be observed that the value of input is inverted when the value of flag is 1.

Consider the number 57. The binary expansion of 57 is 111001.

1 1 1 0 0 1

Consider a new number 1.

0 0 0 0 0 1

To toggle the Least Significant bit or left most bit, perform 57 ^ 1 which results in

1 1 1 0 0 0

The number 111000 is generated.

Now to toggle the last bit, we modify the number 1 such that now the last bit is set instead of the first bit. To do so we must left shift 1 by log2(n) places or in this case log2(57) which is basically 5. After doing so we get,

1 0 0 0 0 0

Computing XOR now gives us.

0 1 1 0 0 0

The number 01100 is generated which is equal to 24. Upon comparing it with the binary expansion of the original number 57 which was 111001, it can be observed that the first and the last bit have been toggled in the final answer.

Hence the answer is 24.

Algorithm

Function toggle_first_bit()

  • Compute n ^ 1

  • Update n

Function toggle_last_bit()

  • Initialise r = log2(n)

  • Initialise k = 1 << r

  • Compute n ^ k

  • Update n

Function main()

  • Initialise n

  • If (n == 1)

    • return 0;

  • Function call toggle_first_bit().

  • Function call toggle_last_bit().

  • Display n.

Example: C++ Program

This program modifies an input number n by toggling the first and the last bit of its binary expansion. It employs bitwise operator XOR and left shift operator to achieve its goal.

// This C++ program toggles the first and the last bit of a number
#include <iostream>
#include <cmath>
using namespace std;
// this function flips the last bit of the number
// it uses the concept that a log(n) bits are used in the binary expansion of a number n
void toggle_last_bit(int& n){
   int r = log2(n); // value of r indicates the count of last bit of n
   int k; // generate a number with log(n) where only the last bit is 1 using the left shift operator
   k = 1 << r;
   n = n ^ k; // toggle the last bit of n by computing n XOR k
}

// this function flips the first bit of the number by computing n XOR 1
void toggle_first_bit(int& n){
   n = n ^ 1;
}
int main(){
   int n = 113;
   cout << "input number = 113" << endl;
   if(n == 1){
      cout << "0";
      return 0;
   }
   toggle_first_bit(n);  // function call to toggle first bit
   toggle_last_bit(n); // function call to toggle last bit
   cout << "Number after Toggle First and Last Bits of a Number: "<<n;
   return 0;
}

Output

input number = 113
Number after Toggle First and Last Bits of a Number: 48

Time and Space Analysis

Time Complexity − O(1), since the algorithm always works in constant time independent of the input number.

Space Complexity − O(1), since no auxiliary space is used in the implementation.

Conclusion

This article discusses an approach to toggle the first and the last bit of a number. To do so we employed the use of the bitwise left shift operator to generate a new bit pattern and the bitwise XOR operator to compute the result. The concept of the approach, dry run of examples, algorithm used, C++ program solution as well as the time and space complexity analysis was explained thoroughly for a deeper understanding.

Updated on: 17-Aug-2023

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements