How to Set, Clear, and Toggle a Single Bit in C++?Bitwise operators are primarily used in low-level programming. Bitwise operators allow us to set, check, clear, or toggle bits in an integer type. In embedded systems, bitwise operators perform bitwise operations on a single bit of a port or register. In this article, we will discuss how to set, clear, and toggle a single bit in C++. 1. Setting Bit:Setting the Nth bit means that if it is 0, it is set to 1; if it is 1, it is unchanged. In C++, the bitwise OR operator (|) is used to specify the bits of an integer data type. As we know, "|" (bitwise OR operator) evaluates a new integer value with each bit position being 1 only if the operand (integral type) has a 1 in that position. Example:Let us take a C++ program to implement the setting of the bit. Output: Setting up the third bit in the given number: 18 The resultant value = 26 Explanation: In this example, the Program defines a characteristic called setting_bit which takes parameters: number(the input val) and pos (the position of the bit to be set). In the primary function, an instance range 18 is initialized and saved in the variable number. After that, the program calls the setting_bit method and passes the number and the position as arguments. Inside the setting_bit function, the bitwise OR operator (within the wide variety. The expression (1 << pos) calculates a cost with an nth bit at position pos set to one and all different bits set to zero. The bitwise OR operation with the original number units the corresponding bit in the range to at least one if it was at the beginning zero. Otherwise, it leaves it unchanged. The resulting function is used to display the number, which is set at the position using the cout statement. 2. Clearing a Bit:Clearing a bit means setting it to 0 when it is 1 without touching or affecting other bits. This task is accomplished by using the bitwise AND and negation operator (bitwise NOT). Bitwise NOT inverts all bits from 1 to 0 and from 0 to 1. This bitwise NOT property is useful for clearing set bits. Example:Let us take a C++ program to implement the Clearing of the bit. Output: Clearing up the third bit in the given number: 18 The resultant value = 26 Explanation: In this example, the program defines a function called clearing_bit, which takes parameters: the number (the enter range) and pos (the position of the bit to be cleared). Inside the clearing_bit method, the bit at position pos inside the range is cleared using a bitwise AND operation with a bitmask. The bitmask is created by using the bitwise NOT operator (~) on the result of shifting 1 to the left by way of post positions. It sets the bit at position pos to zero and leaves all other bits unchanged. The resulting number is returned from the clearing_bit function. In the primary feature, an instance range 12 is initialized and stored in the variable number. The clearing_bit function is known with the number and the position 3 as arguments, and it returns 8 as output. 3. Toggling bit:When a bit is toggled, the bit is in its complement state. If the bit is currently set, change it to unset and vice versa. We can use the bitwise XOR ^ operator to change a bit. The bitwise XOR operator changes to 1 if the corresponding bits of both operands are different and 0 if they are the same. Example:Let us take a program to implement the toggling of the bit in C++. Output: Toggling up the third bit in the given number: 16 The resultant value = 24 Explanation: In this example, the program uses a function known as toggling_bit, which takes parameters: quantity (the enter range) and pos (the placement of the bit to be toggled). Inside the toggling_bit method, the bit at function pos within the number is toggled using a bitwise XOR operation (^) with a bitmask. The bitmask is created by shifting 1 to the left through pos positions. It creates a value with the simplest bit at function pos set to 1 and all other bits set to zero. The bitwise XOR operation toggles the bit at function pos, which changes it to 0 if it was originally 1 and adjustments it to at least one if it becomes in the beginning zero. The resulting value is returned from the toggling_bit function. In the above example method, input number 16 is initialized and saved within the variable variety, and returns the output as 24. Next TopicLookup Tables in C++ |
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