{"id":23240,"date":"2023-12-17T12:08:25","date_gmt":"2023-12-17T12:08:25","guid":{"rendered":"https:\/\/linuxways.net\/?p=23240"},"modified":"2024-03-25T02:10:29","modified_gmt":"2024-03-25T02:10:29","slug":"bitwise-operators-c-language","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/bitwise-operators-c-language\/","title":{"rendered":"Bitwise Operators in C Language"},"content":{"rendered":"<div id=\"wpbody\">In this Linux Ways article, you&#8217;ll learn how to use the bitwise operators in C. We&#8217;ll show you a section that is dedicated to each of these operators where you&#8217;ll see their symbol, their expression, a truth table describing their logic, and a practical example with code snippets and images that show their usage.<\/p>\n<p>You will also see how to assign the values in binary notation and we will show you the code of a function that displays in this notation the value of the variables that we will use in different examples.<\/p>\n<p>You&#8217;ll also learn about short-circuit logic or minimum evaluation in bit-by-bit logic operations, so you&#8217;ll have a comprehensive knowledge and know which the best option for each need is when you need to perform the bitwise operations in C.<\/p>\n<h2><a id=\"post-23240-_r0cioic0h3xf\"><\/a>Bitwise Operators in the C Language<\/h2>\n<p>Bitwise operations can be logical or bit-shifting. The logical operations of this type are performed bit by bit on the binary basis of the variable. For example, in a logical operation between variables \u201ca\u201d and \u201cb\u201d that returns the result in \u201cc\u201d, the bit \u201c0\u201d of variable \u201ca\u201d operates on bit \u201c0\u201d of variable \u201cb\u201d and returns the result in bit \u201c0\u201d of variable \u201cc\u201d. In the following, we will see a table of bitwise operations and their operators in C:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Operation<\/strong><\/td>\n<td><strong>Operator<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>NOT<\/strong><\/td>\n<td><strong>~<\/strong><\/td>\n<td>Returns the inverted value of a variable.<\/td>\n<\/tr>\n<tr>\n<td><strong>OR<\/strong><\/td>\n<td><strong>|<\/strong><\/td>\n<td>Perform an OR operation between two variables.<\/td>\n<\/tr>\n<tr>\n<td><strong>AND<\/strong><\/td>\n<td><strong>&amp;<\/strong><\/td>\n<td>Perform an AND operation between two variables.<\/td>\n<\/tr>\n<tr>\n<td><strong>XOR<\/strong><\/td>\n<td><strong>^ <\/strong><\/td>\n<td>Perform an XOR operation between two variables.<\/td>\n<\/tr>\n<tr>\n<td><strong>Left shift<\/strong><\/td>\n<td><strong>&lt;&lt;<\/strong><\/td>\n<td>Shift <strong><em>n<\/em><\/strong> bits to the left.<\/td>\n<\/tr>\n<tr>\n<td><strong>Right shift<\/strong><\/td>\n<td><strong>&gt;&gt;<\/strong><\/td>\n<td>Shift <strong><em>n<\/em><\/strong> bits to the right.<\/td>\n<\/tr>\n<tr>\n<td><strong>AND<\/strong><\/td>\n<td><strong>&amp;&amp;<\/strong><\/td>\n<td>Short circuit AND operation.<\/td>\n<\/tr>\n<tr>\n<td><strong>OR<\/strong><\/td>\n<td><strong>||<\/strong><\/td>\n<td>Short circuit OR operation.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The logical NOT and shift operations are unary, which means that they operate on a single variable while the AND, OR, and XOR operations operate on two.<\/p>\n<p>The bitwise operations are often used, for example, to mask multiple flags in a single variable which is then used as a flag input argument in functions.<\/p>\n<p>To better understand the bitwise operations that we&#8217;ll see in this article, the next section provides tips to help you easily assign and display the values on the screen using binary notation.<\/p>\n<h2><a id=\"post-23240-_fv916gesh2q7\"><\/a>How to Assign and View the Values with Binary Notation in the C Language<\/h2>\n<p>To assign the binary values to the variables in the C language, we must prefix the binary value with the notation base which is \u201c0b\u201d. In the following, we will see a fragment that declares the variable \u201ca\u201d of type unsigned char and assigns it with the value of 00011000.<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b00011000<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>Now, let&#8217;s look at how we can make these values visible in the command console. The code that we see in the following belongs to a function that converts the unsigned char values that are sent in its input argument into binary notation strings and displays them on the screen:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"kw4\">void<\/span> tobin <span class=\"br0\">&#40;<\/span><span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> tobin <span class=\"br0\">&#40;<\/span><span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> a<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> mask<span class=\"sy0\">=<\/span> <span class=\"nu6\">0b10000000<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> c<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> bit<span class=\"sy0\">=<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span> bit<span class=\"sy0\">!=<\/span><span class=\"nu0\">8<\/span><span class=\"sy0\">;<\/span> bit<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; c <span class=\"sy0\">=<\/span> a <span class=\"sy0\">&amp;<\/span> mask<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span> c<span class=\"sy0\">==<\/span> <span class=\"nu0\">0<\/span> <span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a><span class=\"br0\">&#40;<\/span> <span class=\"st0\">&quot;0&quot;<\/span> <span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a><span class=\"br0\">&#40;<\/span> <span class=\"st0\">&quot;1&quot;<\/span> <span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; mask <span class=\"sy0\">=<\/span> mask <span class=\"sy0\">&gt;&gt;<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; c <span class=\"sy0\">=<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"br0\">&#125;<\/span> &nbsp;<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; <span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>We store this simple function in a file named \u201ctobin.c\u201d in the directory where the main file is located, and it will help us see the results in binary notation of the examples that we will see in this article.<\/p>\n<p>To use the function, we need to include it to the main file as follows:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &quot;tobin.c&quot;<\/span><\/div><\/div>\n<h2>\nThe \u201c~\u201d Operator and Logical Bitwise NOT Operations<\/h2>\n<p>The operator that is used to perform the logical NOT operations is the \u201c~\u201d symbol. In the following, we see the expression that is used to perform this operations:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span>~ a<span class=\"sy0\">;<\/span><\/div><\/div>\n<p>The NOT operation inverts the value of the bit, giving 1 if the operated bit is 0, and 0 if it is 1. This type of operation is used to get the complement of a value. In the following, we see the truth table for this operation:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>a<\/strong><\/td>\n<td><strong>~<\/strong><\/td>\n<td><strong>=<\/strong><\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td><strong>~<\/strong><\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><strong>~<\/strong><\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>How to Perform a NOT Operation in the C Language<\/h2>\n<p>In this example, we will create the variables \u201ca\u201d and \u201cc\u201d, both of type unsigned char. The variable \u201cc\u201d stores the result of the NOT operation of the variable \u201ca\u201d. The binary value that we assign to this variable is 0b00011000 whose conversion to an integer is 24.<\/p>\n<p>Then, we execute the NOT operation with the following expression:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span>~ a<\/div><\/div>\n<p>Using the tobin() function, we display the original and resulting value on the screen in binary format. In the following, we see the code for this operation:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;stdlib.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &quot;tobin.c<\/span><br \/>\n<br \/>\n<span class=\"kw4\">int<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b00011000<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> c<span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\nc <span class=\"sy0\">=<\/span>~a<span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>In the following figure, we see the execution of this code, returning the value of \u201ca\u201d and the result of the operation in \u201cc\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23245\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-1.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-1-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-1-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23240-_8puq0nib31j2\"><\/a>The \u201c&amp;\u201d Operator and Logical AND Operations in the C Language<\/h2>\n<p>The operator that is used to perform the logical AND operations is the \u201c<em>&amp;<\/em>\u201d symbol.<\/p>\n<p>In the following, we see the expression that is used to perform these logical operations between variables \u201ca\u201d and \u201cb\u201d:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span> a <span class=\"sy0\">&amp;<\/span> b<span class=\"sy0\">;<\/span><\/div><\/div>\n<p>The AND operation returns 1 only when bits \u201ca\u201d and \u201cb\u201d are equal to 1, and 0 in other cases. In the following, we see the truth table for the AND operation:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>a<\/strong><\/td>\n<td><strong>&amp;<\/strong><\/td>\n<td><strong>b<\/strong><\/td>\n<td><strong>=<\/strong><\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td><strong>&amp;<\/strong><\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td><strong>&amp;<\/strong><\/td>\n<td>1<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><strong>&amp;<\/strong><\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><strong>&amp;<\/strong><\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The logical AND operations are commonly used to clear the bits from a flag mask. For example, if we want to set the bit one of a mask to zero, we perform this operation between the mask and the \u201c0b1111101\u201d value. In this way, bit one is set to 0 while the rest retains its previous state.<\/p>\n<h2><a id=\"post-23240-_woimt6h4bbeo\"><\/a>How to Perform a Logical AND Operation in the C Language<\/h2>\n<p>In this example, we will see how to perform the AND operation between variables \u201ca\u201d and \u201cb\u201d and store the result in variable \u201cc\u201d.<\/p>\n<p>We assign the \u201c0b11101111\u201d to variable \u201ca\u201d and the \u201c0b01010011\u201d value to variable \u201cb\u201d. Then, we perform the AND operation with the \u201c&amp;\u201d operator. To display the values and results in binary notation, we call the tobin() function. In the following, we see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;height:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;stdlib.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<span class=\"co2\">#include &quot;tobin.c&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b11101111<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> b <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b01010011<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> c<span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;--------<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\nc <span class=\"sy0\">=<\/span> a <span class=\"sy0\">&amp;<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The image that we see in the following shows the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23247\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-2.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-2-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-2-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we can see, using the AND operations, we can clear the bits from a mask, this being the method to deselect the flags in variables that group several of them.<\/p>\n<h2><a id=\"post-23240-_nrrh4yrgon52\"><\/a>The \u201c|\u201dOperator and Logical OR Operations<\/h2>\n<p>The operator to perform the logical OR operations is the \u201c|\u201d symbol. In the following, we see the expression that is used to perform this logical operations between bits \u201ca\u201d and \u201cb\u201d:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span> a <span class=\"sy0\">|<\/span> b<span class=\"sy0\">;<\/span><\/div><\/div>\n<p>The OR operation results in 1 if bit \u201ca\u201d or bit \u201cb\u201d or both are equal to 1 and 0 if bit \u201ca\u201d and bit \u201cb\u201d are both equal to 0. In the following, we see the truth table for the OR operation:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>a<\/strong><\/td>\n<td><strong>|<\/strong><\/td>\n<td><strong>b<\/strong><\/td>\n<td><strong>=<\/strong><\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td><strong>|<\/strong><\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td><strong>|<\/strong><\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><strong>|<\/strong><\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><strong>|<\/strong><\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The bitwise OR operation is often used to create the flag masks in variables, grouping several of them.<\/p>\n<h2><a id=\"post-23240-_q61tt9usukb7\"><\/a>How to Perform an OR Operation in the C Language<\/h2>\n<p>&nbsp;<\/p>\n<p>In this example, we will show you how to perform an OR operation between variables \u201ca\u201d and \u201cb\u201d, both of type unsigned char, and store the result in the variable \u201cc\u201d. We will assign the \u201c0b000011\u201d value to variable \u201ca\u201d and the \u201c0b010101\u201d value to variable \u201cb\u201d. Then, we perform the OR operation with the \u201c|\u201d operator.<\/p>\n<p>Then, we call the tobin() function to see the status of the variables and its result. In the following, we see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;height:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;stdlib.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &quot;tobin.c&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b00000011<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> b <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b01010101<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> c<span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;--------<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\nc <span class=\"sy0\">=<\/span> a <span class=\"sy0\">|<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The image that we see in the following shows the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23250\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-3.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-3-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-3-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>As we see, the use of OR operations is the method to configure the flags in variables by grouping several of them.<\/p>\n<h2><a id=\"post-23240-_rvr1lbuyiub8\"><\/a>The \u201c^\u201d Operator and Logical XOR Operations<\/h2>\n<p>The operator to carry out the XOR logical operations is the \u201c|\u201d symbol.<\/p>\n<p>In the following, we see the expression that is used to perform the XOR logical operations between bits \u201ca\u201d and \u201cb\u201d:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span> a <span class=\"sy0\">^<\/span> b<span class=\"sy0\">;<\/span><\/div><\/div>\n<p>The XOR operation results in 1 if bit \u201ca\u201d or bit \u201cb\u201d are equal to 1 and 0. But it results in 0 if both are equal to 0 or equal to 1. In the following, we see the truth table for the XOR operation:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>a<\/strong><\/td>\n<td><strong>^ <\/strong><\/td>\n<td><strong>b<\/strong><\/td>\n<td><strong>=<\/strong><\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td><strong>^ <\/strong><\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td><strong>^ <\/strong><\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><strong>^ <\/strong><\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><strong>^ <\/strong><\/td>\n<td>1<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><a id=\"post-23240-_ytjqldc4co77\"><\/a>How to Perform a XOR Operation in the C Language<\/h2>\n<p>&nbsp;<\/p>\n<p>In this example, we will show you how to perform a XOR operation between variables \u201ca\u201d and \u201cb\u201d, both of type unsigned char, and store the result in variable \u201cc\u201d. We assign the \u201c0b11010011\u201d value to the variable \u201ca\u201d and the \u201c0b01010101\u201d value to \u201cb\u201d. Then, we operate the variables with the \u201c^\u201d operator and show the state of it with the tobin() function. In the following, you can see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;height:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;stdlib.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &quot;tobin.c&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b11010011<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> b <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b01010101<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> c<span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<a href=\"http:\/\/www.opengroup.org\/onlinepubs\/009695399\/functions\/printf.html\"><span class=\"kw3\">printf<\/span><\/a> <span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;--------<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\nc <span class=\"sy0\">=<\/span> a <span class=\"sy0\">^<\/span> b<span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>b<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>c<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The image that we see in the following shows the execution of this code:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23253\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-4.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-4-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-4-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/strong><\/p>\n<p>As we can see, the XOR operation behaves like an OR operation, except in cases where \u201ca\u201d and \u201cb\u201d are both equal to 1.<\/p>\n<h2><a id=\"post-23240-_i1d2by90eqvv\"><\/a>The \u201c&lt;&lt;\u201d and \u201c&gt;&gt;\u201d Operators for Bit Shifting<\/h2>\n<p>The \u201c&lt; &lt;\u201d and \u201c&gt; &gt;\u201d operators shift a number \u201cn\u201d bits of a variable to the left or right and fill the empty places with zeros.<\/p>\n<p>The following is the expression that is used to perform the n-bit shifts to the left and to the right:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span> a <span class=\"sy0\">&lt;&lt;<\/span> n<span class=\"sy0\">;<\/span><br \/>\n<br \/>\nc <span class=\"sy0\">=<\/span> a <span class=\"sy0\">&gt;&gt;<\/span> n<span class=\"sy0\">;<\/span><\/div><\/div>\n<h2>\n<p>How to Shift the Bits with the \u201c&lt;&lt;\u201d and \u201c&gt;&gt;\u201d Operators in the C Language<\/h2>\n<p>In this example, we will declare the variable \u201ca\u201d and assign it with the \u201c0b0011000\u201d value. Then, we will rotate two bits to the left and display the result on the screen with the tobin() function. Then, we move the bits again, but four positions to the right, and display the result. In the following, we can see the code for this example:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;height:100%;\"><div class=\"c codecolorer\"><span class=\"co2\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;stdlib.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &lt;unistd.h&gt;<\/span><br \/>\n<br \/>\n<span class=\"co2\">#include &quot;tobin.c&quot;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">void<\/span> main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">unsigned<\/span> <span class=\"kw4\">char<\/span> a <span class=\"sy0\">=<\/span> <span class=\"nu6\">0b00011000<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\na <span class=\"sy0\">=<\/span> a <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\na <span class=\"sy0\">=<\/span> a <span class=\"sy0\">&gt;&gt;<\/span> <span class=\"nu0\">4<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\ntobin<span class=\"br0\">&#40;<\/span>a<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The image that we see in the following shows the execution of this code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" class=\"wp-image-23255\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-5.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-5-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/12\/word-image-23240-5-768x576.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h2><a id=\"post-23240-_a47e9m3wxxzg\"><\/a><br \/>\nLogical Short Circuit Operations in the C Language<\/h2>\n<p>The bitwise operations are often used as input conditions in \u201cif\u201d conditions or even as escape conditions in loops. In these cases, we can perform a simple or compound bitwise operation.<\/p>\n<p>When we use the compound operations, there are cases where the first expression determines the final result independently of the result of the other operations. We can see this in the following expression:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span> a <span class=\"sy0\">&amp;<\/span> b <span class=\"br0\">&#41;<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"br0\">&#40;<\/span> d <span class=\"sy0\">&amp;<\/span> e <span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>Now, we replace the variables with bit values:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span> <span class=\"nu0\">0<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"nu0\">1<\/span> <span class=\"br0\">&#41;<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"br0\">&#40;<\/span> <span class=\"nu0\">1<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"nu0\">1<\/span> <span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>In this case, the result in \u201cc&#8221; is defined by the result =0 of the AND operation between \u201ca\u201d and \u201cb\u201d, independent of the result of the second operation.<\/p>\n<p>The same happens with the following OR operation:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span> <span class=\"nu0\">0<\/span> <span class=\"sy0\">|<\/span> <span class=\"nu0\">1<\/span> <span class=\"br0\">&#41;<\/span> <span class=\"sy0\">|<\/span> <span class=\"br0\">&#40;<\/span> <span class=\"nu0\">1<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"nu0\">1<\/span> <span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p>In this expression, the result of the OR operation between \u201ca\u201d and \u201cb\u201d is equal to 1, which is why it also determines the result in \u201cc\u201d. While the result of the second operation has no influence on the final result.<\/p>\n<p>For these cases, there is the possibility of performing the short-circuit operations. In this type of operation, the first expression is analyzed. If the result determines the final result, the program ignores the other expressions.<\/p>\n<p>To perform the short-circuit operations, the double operators of the operation to be performed are used. We can see an expression that applies the short circuit logic in a compound AND operation in the following:<\/p>\n<div class=\"codecolorer-container c blackboard\" style=\"width:100%;\"><div class=\"c codecolorer\">c <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span> <span class=\"nu0\">0<\/span> <span class=\"sy0\">|<\/span> <span class=\"nu0\">1<\/span> <span class=\"br0\">&#41;<\/span> <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"br0\">&#40;<\/span> <span class=\"nu0\">1<\/span> <span class=\"sy0\">&amp;<\/span> <span class=\"nu0\">1<\/span> <span class=\"br0\">&#41;<\/span><\/div><\/div>\n<h2><a id=\"post-23240-_ykvc0esh2zuw\"><\/a><br \/>\nConclusion<\/h2>\n<p>In this Linux Ways article, we showed you how to use the operators available in C to perform the bitwise operations. In this article, we learned a table with the different operators available and then a section for each of them describing their function, the expression used, and how to use them with practical examples. We also provided you the tips that you need to handle the binary numbers and see their values on the screen so you can quickly understand the bitwise operations.\n<\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>Comprehensive tutorial on how to use the available operators in C to perform the bitwise operations, their function, the expression used, and how to use them.<\/p>","protected":false},"author":110,"featured_media":23297,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-23240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=23240"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/23240\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/23297"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=23240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=23240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=23240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}