Home > Backend Development > C++ > Switch the first and last digits of a number

Switch the first and last digits of a number

PHPz
Release: 2023-08-27 17:57:07
forward
1086 people have browsed it

Switch the first and last digits 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
Copy after login
Output: 4
Copy after login
The Chinese translation of

Explanation

is:

Explanation

The binary expansion of 13 is 1101.

After switching the first and last bits, the extension becomes 0100, which is equal to 4.

Therefore, the output result is 4.

Input: 27
Copy after login
Output: 10
Copy after login
The Chinese translation of

Explanation

is:

Explanation

The binary expansion of 27 is 11011.

After switching the first and last bits, the extension becomes 01010, which is equal to 10.

Hence the output is 10.

Input: 113
Copy after login
Output: 48
Copy after login
The Chinese translation of

Explanation

is:

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 digit of a number n, we calculate n^1. It performs an XOR operation, inverting the least significant or first bit of n with 1.

To flip the last digit, we generate a number k with only the last digit set. The position r of the last bit 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.

  • Switch the last digit of the number by XORing n with 1<th bit of the number.

  • Display the answer.

dry run

First let us understand how the bitwise XOR (^) operator works.

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

It can be observed that when the value of flag is 1, the input value will be reversed.

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 or leftmost bit, do 57 ^ 1, the result is

1 1 1 0 0 0

The number 111000 is generated.

Now, to switch the last digit, we modify the number 1 so that the last digit is set instead of the first. In order to do this, we have to shift 1 to the left by log2(n) places, or in this case log2(57), which is 5. After doing this we get:

1 0 0 0 0 0

Computing XOR now gives us.

0 1 1 0 0 0

生成了数字01100,等于24。将其与原始数字57的二进制展开进行比较,可以观察到最终答案中的第一位和最后一位已经被切换。

因此,答案是24。

算法

函数 toggle_first_bit()

  • Compute n ^ 1

  • 更新 n

函数 toggle_last_bit()

  • 初始化 r = log2(n)

  • 初始化 k = 1 << r

  • 计算 n ^ k

  • 更新 n

Function main()

  • 初始化 n

  • 如果 (n == 1)

    • return 0;

  • Function call toggle_first_bit().

  • 调用函数 toggle_last_bit()。

  • 显示 n.

示例:C++程序

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;
}
Copy after login

输出

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

Time and Space Analysis

时间复杂度 - O(1),因为该算法始终在常数时间内工作,与输入数字无关。

空间复杂度 - O(1),因为在实现中没有使用辅助空间。

Conclusion

本文讨论了一种切换数字的第一个和最后一个位的方法。为了实现这一点,我们使用了位左移运算符来生成新的位模式,使用位异或运算符来计算结果。为了更深入地理解,详细解释了方法的概念、示例的演示、使用的算法、C++程序解决方案以及时间和空间复杂度分析。

The above is the detailed content of Switch the first and last digits of a number. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template