C Programming

Using & Operator in C

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.

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:

$ sudo apt install gcc

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:

$ touch test.c

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:

$ nano test.c

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.

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.

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

$ gcc test.c

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:

$ ./a.out

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:

$ nano test.c

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.

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.

Compile your code first via the “gcc” compiler:

$ gcc test.c

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:

$ ./a.out

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:

$ nano test.c

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:

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

Compile your code once again with the gcc compiler:

$ gcc test.c

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.

$ ./a.out

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

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.