C Programming

Bitwise operators in C Language

Bitwise operators, often known as bit-level coding, have been cast-off to interpret data only at the bit level. Bitwise performs operations on one or even additional bit patterns and binary numbers on the bit level. These have been used to speed up the estimation progression of numerical calculations. It consists of two numbers, one of which is 0 and the other is 1. Here are some bitwise operators which we will discuss in our article.

Login from your Linux system and try the “Ctrl+Alt+T” shortcut to open the console terminal. We will have some examples to elaborate on the operations of bitwise operators in the C language. Let’s elaborate on each bitwise operator separately.

Bitwise AND:

The bitwise operator has been used to replicate a bit to the result if that bit is present in both the mutual operands. C or C++ requires 2 operands that perform AND on each bit of those two integers. Bitwise AND outcomes 1 when both the bits have a value of 1. So, to understand the working, create and open a C type file using Nano editor. For that, we have to be consuming “nano” instruction in the shell as follows:

$ nano test.c

The code for the Bitwise AND operator has been displayed in the image below. All you have to do is just write this code in your GNU nano editor file “test.c” as it is. This code has the stdio.h header library without which we cannot get the coding done in C programming. Then we have created a main method with its return type as integer. In the C language, the execution of the code is done via the main method. So we have declared two integer type variables, “x” and “y,” with the values “35” and “13” accordingly. After that, another integer variable has been specified with zero as its value. We will be using the first two integer variables and apply the bitwise AND operator between them. This work has been done on the next line where the AND operator has been used as “&,” and the resultant value will be stored in the null variable “z.” Then we have used the print statement to show the resultant value in the terminal screen, and the main method closes. Save your file using the GNU shortcut “Ctrl+S” and then quit the nano editor via “Ctrl+X” from the keyboard typewriter.

So, it’s the moment to compile the above code after saving the file. Use the “gcc” instruction in your console shell while using the name of a file as “test.c” or whatever you named the file and hit the Enter key. You can see it shows no error; this means the code is manually correct.

$ gcc test.c

Having compiled the code, it’s time to execute the code now. For this purpose, run the stated-below query in the shell. The output is showing “1” as an outcome. This means both of our integer variables have “1” in one of their “bits.” This is why it returns “1”.

$ ./a.out

Bitwise OR:

Now, it’s turned for the bitwise OR operator to be elaborated. The bitwise operator returns “1” as a result when one of its bit is 1. If both the bits of two integers are 0, then it yields 0. After getting all the bits, there will be a set of bits generated. We have to see what that number those bits formed. So, open the same test.c document first. Type and then save the shown-below code in the GNU file using the “Ctrl+S” key. The code is almost the same as of above bitwise AND operator example. This time we have just changed the value of integer “x” to 47, and we have used the OR operator, e.g. “|” between both the variables. Quit the file to compile the code.

After successfully executing the document “test.c”, it shows the “47” output bit.

$ ./a.out

Bitwise XOR:

The bitwise operator returns 1 when both of the number bits are different. So when the bits are the same, it will yield 0. The “^” operator sign will represent the bitwise operator. So again, open the document and write the same old code in the “GNU” file editor. This time we have been using the “^” operators within two integer variables, and the result will be stored in the “z” variable while printing out in the terminal.

Compile and Running the file “test.c” return “34” as a result. This means the new integer “34” is generated after the “XOR” operator has been applied to two integer variables.

$ ./a.out

Bitwise Complement:

This operator will only be applied to a single variable, and it will revert the value of the bit number. For example, it will change the 0 bit to 1 and 1 to 0 bit. Write the same code in the same file but with a little change at line 6. We have assigned the reverse of “x” to “z.”

Compile the file and run it. In C, the result of Bitwise complement has been incremented with 1 with a negative sign.

Left Shift Operator:

It will shift the place of bits to some extent. The same code can be displayed in the image appended- below. In this example, we will be shifting 2 bits to the left.

The output shows the “188” as a newly generated value.

$ ./a.out

Right Shift Operator:

It works the same as the left shift works but in the opposite direction, as shown in the code below.

This time we have got “11” as output upon shifting 2 bits to the right side.

$ ./a.out

Conclusion:

We have covered all the basic 6 bitwise operators in our C language code in this article. I hope you get what you wanted from our guide.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.