Toggle the First and Last Bits of a Number in C++13 May 2025 | 4 min read Given a number n, flip the numbers so that the first and last bits of the binary expansion of the new number are the same; that is, if the bit that was originally assigned is 1, the flipped bit should be assigned 0, etc. It is necessary to leave the elements in between the first and last bits unchanged. Bitwise operators can be applied to work with individual bits in binary quantities or bit patterns. For Example: 1. Input = 12 Output = 3 Explanation: 12 -> Binary Expansion of input 12 is 1100 After toggling the first and last bit of binary number 12, it becomes 0011. 0011 à 3 Hence, on toggling the first and last bits of the given input, the output will be 3. 2. Input = 30 Output = 15 Explanation: 30 -> Binary Expansion of input 30 is 11110. After toggling the first and last bit of binary number 30, it becomes 01111. 01111à 15 Hence, on toggling the first and last bits of the given input, the output will be 15. 3. Input = 145 Output = 16 Explanation: 145 -> Binary Expansion of input 145 10010001 After toggling the first and last bit of binary number 145, it becomes 00010000. 00010000 à 16 Hence, on toggling the first and last bits of the given input, the output will be 16. Approach:This method uses the left shift operator and bitwise XOR. The bitwise XOR operator evaluates to 1 if the corresponding bit of both operands are different, and evaluates to 0 otherwise. We'll make use of the bitwise XOR operator's bit-toggling functionality. For example, if the integer n has a first bit of 1, n ^ 1 will result in a first bit of 0. Furthermore, the operation n ^ 1 will alter the number's initial bit from 0 to 1 if it is currently set to 0. We compute n ^ 1 to flip the first bit of the number n. In order to invert the value, it does an XOR operation between the Least Significant Bit and the first bit of n with 1. We create a number, k, in which just the last bit is set, use that number to flip the last bit. The final bit, r, has a location equal to log2(n). It is because the binary expansion of n uses log2(n) bits. Algorithm:
Example 1:Let us take a C++ program to toggle the first and last bits of a number using XOR: Output: ![]() Explanation:
Complexity Analysis:Time Complexity:Its time complexity is O(1) because the method operates in a fixed amount of time regardless of the quantity of inputs. Space Complexity:Its space complexity is O(1) because no auxiliary space is utilized in the implementation. Next TopicObject-pool-design-pattern-in-cpp |
Shaker Sort in C++
Introduction Sorting methods are crucial in the realm of computer science and impact various areas, including data analysis, database management systems, and everyday tasks such as organizing files on your computer. Shaker Sort, also known as Cocktail Sort or Ripple Sort, is one of the sorting algorithms...
7 min read
C++ code to find an index where this is a hash collision
In this article, we will discuss how to find an index where this is a hash collision in C++ with several examples. Problem Statement: Assume we have a number a and an array P, which has n elements in it. There is a hash table with 'a' buckets...
5 min read
Finite and Infinite Recursion in C++
Recursion is one of the core concepts of computer science and programming, where a function calls the same function to solve the given problem. The approach is very effective in solving problems that can be divided into several similar problems with the same solution. Iterative...
9 min read
std::basic_streambuf::sputbackc in C++
In C++, the std::basic_streambuf class is part of the Standard Library's input/output (I/O) stream structure. It offers an interface for reading and writing character sequences representing various types of streams, such as files, network connections or memory buffers. A crucial function found in the std::basic_streambuf class...
6 min read
2Sum in C++
2Sum is a traditional algorithmic problem known in the computer science and programming world. The problem is fundamental to students in data structures, algorithm design, and computational complexity. But all the same, the problem appears to encapsulate a number of important concepts and techniques that could...
11 min read
Centred Heptagonal Number in C++
A centred heptagonal number is a centred polygonal number that has the shape of a heptagon as well as a dot in the center. Regular heptagonal numbers grow by adding whole layers to form a heptagon, but centred heptagonal numbers grow outward symmetrically from just...
8 min read
dlsym() function in C++
In this article, we will discuss the with its syntax, return value, and examples. What is the dlsym() Function? The address of a symbol declared inside an object made available by a dlopen() call is obtained by the dlsym() function. The name parameter will represent the character...
3 min read
Difference between C++ and Erlang
In the list of programming languages, every language is designed for certain objectives and applications. Two such languages include C++ and Erlang; they represent wholly different approaches to development and are oriented toward different scopes of software construction. In this article, we will discuss the...
4 min read
Finding the nth Term of the Cantor Sequence in C++
Introduction One of the notable mathematical sequences is known as the ‘Cantor Sequence’ and is constructed using the fraction representation of a given number grid in a zigzag manner. Cantor sequences are often encountered in various branches of mathematics, such as number theory and even in...
10 min read
Dense Graphs in C++
Introduction Graphs belong to the basic elements used in computer science and mathematics and they signify networks linked by nodes. In graph theory, the graphs are further subdivided into less connected and more connected graphs to help determine the right algorithms and data structures to use. This...
20 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
