C Programming

# Bitwise operators in C with Examples

The bitwise operators have been used to manipulate data only at the bit level. Bit-level computing would be done when you execute bitwise operations. It comprises two numbers, one of which is 0 and the other one is 1. It’s mainly used to speed up mathematical calculations. Inside the C language, we use several sorts of bitwise operators as below. Let’s discuss each one of them one by one. At the time of implementing this guide article, we have been working on Ubuntu 20.04 Linux system. We have successfully logged in from the system and installed the GCC compiler to compile C language code. Without a compiler, we are unable to execute our C script. After that, we have used the Ctrl+Alt+T key shortcut to open the terminal shell, as we have been doing every implementation and execution on a shell.

## Bitwise AND Operator

So, our first example would be of bitwise AND operator. When the matching bits of two numbers or operands are 1, the operator AND is 1. If one of the operand’s bits is 0, the output of such associated bit is also 0. Open your terminal, and use the touch query to create a C-type file in it as below.

\$ touch main.c You can find your file in your Home directory. Open this file by following the below instruction.

\$ nano main.c Now the file has been opened in the GNU editor, write the below simple C code. This code contains a primary function with the library included for standard input and output. After that, we have declared an integer type variable “a” in the primary function and assign it a value. We have then put AND operator condition on the variable “a” along with a number 1. The AND will be calculated, and it will tell whether the output AND is even or odd, and the result will be printed out. Please save the file by Ctrl+S and close it once updated by Ctrl+X. Now compile the code by gcc compiler instruction as below. If gcc commands output nothing, this means that the code is correct. Now execute the file by “a.out” instruction as below. The output shows that the AND operators print Odd as a result.

\$ gcc main.c
\$ ./a.out Let’s take another AND operator. Open the same file again to update the C code in it.

\$ nano main.c The file is opened in GNU editor. Let’s update the code with the below-shown script. We took two integer type variables, “a” and “b.” Assigned both the variables with integer values. In the print statement, we have used the AND “&” operator between both operands to see the AND result on both integers. It will show the result in the shell. Save and close the file. Compile your newly updated file with GCC compiler and run it on a terminal. The output shows the AND result, “2” in the terminal as presented in the picture.

\$ gcc main.c
\$ ./a.out ## Bitwise OR Operator

In this section, we will be discussing the function of OR bitwise operator. When at least a minimum of one matching bit among two numbers is 1, the result of bitwise OR is 1. The bit-wise OR operator is usually represented by “|” in C language. Open your file again to update the code.

\$ nano main.c We have initialized two integer type variables, “a” and “b,” with integer type values assigned to them.  In the print statement, we have used the “|” operator to apply OR between the operands “a” and “b.” Then the result has been printed out via printf statement, and the primary function ends here. Save your code and quit the file. Compilation of the file main.c has been done using the gcc compiler via the shell. After the compilation gets successful, we will be running the file using the “a.out” file in our terminal. The output is showing the result of the OR operator on both operands as below.

\$ gcc main.c
\$ ./a.out ## Bitwise XOR Operator

This section contains the example of the XOR bitwise operator. When the respective bits of two numbers are different, the output of bitwise XOR yields 1. The symbol for XOR is “^.” Hence, open your file once again using the below command.

\$ nano main.c Update the code by taking the exact integers and values “a” and “b.” The difference is as same as in the printf statement. We have changed the symbol between both the integer operands and put “^.”  This symbol represents XOR, and it will calculate the XOR on both the operands and shows the result in the terminal. Again, compile the main.c file first, and then runs the code once again. Compilation and execution get successful, and it returns 29 due to XOr operation on both operands.

\$ gcc main.c
\$ ./a.out ## Bitwise NOT Operator

The bitwise NOT operator has also called the complement operator. The unary complement operator seems to be a bitwise operator that works on solitary one number or operand. It transforms 1 into 0 and 0 into 1. The symbol “~ symbolizes it.” Open your file once again to implement the NOT operator.

\$ nano main.c This time we have updated the symbol to “~,” representing the NOT operator or complement. We have given both the variables, but both have nothing to do with the operator. Compilation and execution get successful and return “-26” as a complement to “-25”.

\$ gcc main.c
\$ ./a.out ## Bitwise Right and Left Shift Operators

The right shift operator moves every bit to the right by a given amount of bits. “>>” is the symbol for it. The left shift operator moves every bit to the left by a fixed amount of bits. The bit locations which the left shift operator has abandoned will be replaced with 0. The left shift operator has been represented by the symbol “<<.” Open your file to update the code for shift operators.

\$ nano main.c In this example, we have taken an integer “x” with a  value. We have taken two “for” loops. The first loop is for the left shift. The value of “x” has been divided by 2 until the loop ends in the left shift. On the other hand, in the right shift, the value of “x” has been multiplied by 2 until the loop ends. Every result of both the shifts has been printed out at every iteration. After the compilation of code, the result is printed out in the shell. The first three lines show the result of the left shift, and the last three lines show the result of the right shift.

\$ gcc main.c
\$ ./a.out Example
Below is the bonus example to illustrate the working of all the operators at a  single glance. You can see we have two variables, and we have applied every bitwise operator on them. Execution of the above code shows the following result.

\$ ./a.out ## Conclusion

We have covered all the bitwise operators in our article, along with suitable examples. We hope you will get no issues regarding any example implemented in this guide. 