Operators are the fundamental concepts of every computer language, and they are used to provide the groundwork for new programmers. Operators are basic symbols that assist us in performing scientific and analytical processes. In C and C , operators are instruments or characters used to execute mathematical, analytical, probabilistic, and bitwise arithmetic computations. Bitwise operators, often recognized as bit-level coding, have been utilized to manipulate data only at the consolidated level. Bitwise performs operations on one or even more data bits or decimal digits only at bit level. These are used to speed up the calculating procedure in arithmetic operations. Bitwise functions cannot be used directly to the primitive data types like float, double, etc. Constantly keep in mind, bitwise operators have been most commonly employed with numerical data types due to their comparability. The bitwise logical operators act a bit at a time on the information, beginning with the lowest relevant ones (LSB), which would be the right side bit, and finding their way to some of the most likely values (MSB), which would be the leftmost piece.

Bitwise AND Operator:

Among the most widely used conceptual bitwise operations is this one. The “&” is a solitary uppercase symbol used to denote it. On either end of the (&) operator, there are two integer statements. When both bits have a bit 1, the outcome of a bitwise AND function is 1; conversely, the outcome is 0. The AND operation has been cleared from the image below. You can see when both x and y are 1, the result is also 1. On the other hand, if one of them is 1 and the other is 0, the result is 0.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-156.png" data-lazy- height="189" src="data:image/svg xml,” width=”316″>

Let’s get started and see some examples of Bitwise AND (&) operator in C language. At the time of this article’s execution, we used the Ubuntu 20.04 Linux operating system. Make sure you have a “gcc” compiler installed on your Linux system to compile your C code. If not, utilize the following command:

Example 01:

Let’s have our first example of elaborating the functioning of AND operator in C language. After the login from the Ubuntu Linux system, you have to open the Terminal shell to create a new C type file. SO, use “Ctrl Alt T” to launch it quickly. Otherwise, you can navigate towards the activity area on the desktop of your Linux system. After the search bar has been opened, write “terminal” and hit Enter. A pop-up application will be opened. Tap on it to launch it. Now the Terminal shell has been opened, let’s create a new C-type file using the touch command in the shell as shown below. We have given the name “test.c” to the C file:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-157.png" data-lazy- height="22" src="data:image/svg xml,” width=”420″>

Now, the file has been created. You can see the newly created file in your home directory of the Ubuntu 20.04 Linux operating system. You can open the file “test.c” using the GNU Nano editor command as below in the terminal. Write the following command and press Enter:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-158.png" data-lazy- height="18" src="data:image/svg xml,” width=”413″>

Now, the test.c file has been released in the GNU Nano editor. Write the below appended C script in it. This code comprises the input-output standard library header. The main function has been utilized to do the functioning. The first printf statement is just used to simply display a Welcome message. In the next line, we have stated two integer-type variables. The value of the variable “x” is higher than that of the variable “y“. Another print statement has been utilized to declare the result of AND operator on both the variables “x” and “y”. After that, the main function closes. Save your Nano file using the “Ctrl S” key and navigate towards the terminal shell again via the “Ctrl X” key.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-159.png" data-lazy- height="203" src="data:image/svg xml,” width=”517″>

Let’s first look at the bit values of both the integers “x” and “y”. When we apply the AND operator on the bit values of both the variables “x” and “y”, it displayed “000000”, which is the bit value of 0. This means our answer should be 0 after the application of AND operator.

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-160.png" height="106" src="data:image/svg xml,” width=”251″>

Let’s compile the C code at the terminal using the “gcc” compiler and the name of a file, appended below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-161.png" data-lazy- height="19" src="data:image/svg xml,” width=”403″>

Now the code has been compiled, let’s run it using the “output” command below. You can see it displays 0 as the result of AND operator on 36 and 16 after the Welcome message:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-162.png" data-lazy- height="53" src="data:image/svg xml,” width=”385″>

Example 02:

Let’s have another example of looking at the functioning of AND operator on some integer values. Open the same “test.c” file using the terminal via the nano editor below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-163.png" data-lazy- height="22" src="data:image/svg xml,” width=”428″>

Let’s update the file “test.c” with the following code. After adding the input and output standard stream in the file, we have used the “main” method with the return type as integer. We have added a printf statement to print the “welcome” message. Another integer type variable, “z”, has been declared with a value of 0. We have applied the AND operator on both the variables and added the result of AND operator in the variable “z”. The last printf statement has been printing the saved result of the AND operator using the variable “z”. Save your code and abandon the nano editor via “Ctrl S” and “Ctrl X” accordingly.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-164.png" data-lazy- height="238" src="data:image/svg xml,” width=”543″>

You can see the bit values of both the integers “50” and “17”. The calculated result of AND operator on both the bit values of “50” and “17” shows that the result will be 16. Let see if it is correct.

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-165.png" height="105" src="data:image/svg xml,” width=”239″>

Compile your code first via the “gcc” compiler:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-166.png" data-lazy- height="20" src="data:image/svg xml,” width=”406″>

Execute the test.c file using the output command as below. You can see that the result is as same as we expected, e.g., 16:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-168.png" data-lazy- height="57" src="data:image/svg xml,” width=”389″>

Example 03:

Let’s have our last example to see the working of AND operator in the C language. Open the file “test.c” once again utilizing the nano editor in the shell:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-170.png" data-lazy- height="15" src="data:image/svg xml,” width=”418″>

Paste the same code in your file below to update it. Again, using the input and output standard header library in our code, we have used the main method with the integer return type. This time we have used the two integers but switched the place of smaller and largest values. Print statement has been utilized to apply the & operator and display the result:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-172.png" data-lazy- height="187" src="data:image/svg xml,” width=”508″>

The bit result of applying AND on both the bit values of integers is 2.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-173.png" data-lazy- height="106" src="data:image/svg xml,” width=”304″>

Compile your code once again with the gcc compiler:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-174.png" data-lazy- height="20" src="data:image/svg xml,” width=”402″>

After compilation of code, simply run the output execution command to see the results. The result is the same as we have mentioned above, e.g., 2.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-175.png" data-lazy- height="36" src="data:image/svg xml,” width=”392″>

Conclusion:

In this article, you have seen the examples of applying AND operator or integer values and how it works on bit values. We hope this article has helped you at its best and you don’t need further guidance on this topic.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Author-Image-150×150.jpg610b52762508c.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.