<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jackson Eshbaugh</title>
    <description>The latest articles on Forem by Jackson Eshbaugh (@jacksoneshbaugh).</description>
    <link>https://forem.com/jacksoneshbaugh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1812120%2Fc3099cf6-9144-4eac-acde-61ae7b31ce60.jpeg</url>
      <title>Forem: Jackson Eshbaugh</title>
      <link>https://forem.com/jacksoneshbaugh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jacksoneshbaugh"/>
    <language>en</language>
    <item>
      <title>An Overview of Bitwise Operations</title>
      <dc:creator>Jackson Eshbaugh</dc:creator>
      <pubDate>Sun, 28 Jul 2024 23:49:21 +0000</pubDate>
      <link>https://forem.com/jacksoneshbaugh/an-overview-of-bitwise-operations-2die</link>
      <guid>https://forem.com/jacksoneshbaugh/an-overview-of-bitwise-operations-2die</guid>
      <description>&lt;p&gt;The following post was taken from a repository I created to help learn (and teach) about bitwise operations. You can find that repo &lt;a href="https://github.com/jacksoneshbaugh/BitwiseOperations" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and I'd suggest checking it out—there's some code examples and solutions there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The goal of this repository is to acquaint you with bitwise operations, explaining what they are, how they work, and what they can be used for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 1: It's All Binary
&lt;/h2&gt;

&lt;p&gt;In C (and most high-level languages), our variables have &lt;em&gt;types&lt;/em&gt;. These types are indicative of a few things. Of course, a variable of type &lt;code&gt;int&lt;/code&gt; will store an integer value, but the key to understanding these bitwise operations is to know that under the hood, all types are stored in memory (anywhere, stack, heap, wherever) as binary. Here's an example of what happens when you store a simple integer value on the stack in C:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After compiling to assembly, the code might look like this (I'm using ARM assembly here, and I've annotated the code using comments):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.section .text
.global main

main:
    ; Set up the stack for the function
    stp x29, x30 [sp, -16]! ; Save previous frame pointer &amp;amp; link register
    mov x29, sp ; Setup a new frame pointer

    ; Initialize x with 2
    ; IN C: int x = 2;
    mov w0, 2 ; Move 2 into the w0 register
    str w0, [sp, 16] ; Store the contents of w0 (2) at a 16-byte offset from the stack pointer
    ; Essentially, the above line stores 2 on the stack.

    mov w0, 0 ; Move 0 into w0, prepare for return

    ; Clear stack
    ldp x29, x30, [sp], 32 ; Restore frame pointer and link register
    ret ; Return
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that most compilers would &lt;em&gt;not&lt;/em&gt; actually store a variable like the one I showed on the stack, as it is unused. However, if it is used multiple times, it would be stored on the stack something like the above.&lt;/p&gt;

&lt;p&gt;If we looked at the location where our variable was stored on the stack (while it is there, of course), we would see something like:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Memory Address&lt;/th&gt;
&lt;th&gt;Value Stored (Hex)&lt;/th&gt;
&lt;th&gt;Value Stored (Binary)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x1000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x02&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00000010&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x1001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00000000&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x1002&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00000000&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x1003&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00000000&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This assumes that your system is little-endian. I won't go into endianness here, but you can read more about it &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Endianness" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The key thing I'd like you to notice about the table above is that even though our integer is only 2 bits long, it takes up 4 bytes (32 bits) of memory. Now, don't freak out—this is normal and expected. One of the many things that C and your compiler do is set standards for the types you invoke. So when I create an &lt;code&gt;int&lt;/code&gt; variable, the compiler knows to allocate 4 bytes (again, 32 bits) of memory. We can even test this using the &lt;code&gt;sizeof()&lt;/code&gt; operator in C. &lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  The &lt;code&gt;sizeof()&lt;/code&gt; Operator
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sizeof()&lt;/code&gt; is not an actual C function. Instead, at compile time, the compiler replaces the expression with the size of the given data type. You can even use this with your own types, like typedefs and/or structs:&lt;/p&gt;


&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt; &lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A Person is %lu bytes long.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/blockquote&gt;

&lt;p&gt;One other thing you might be asking is how negative numbers are stored. Excellent question. Numbers can be &lt;em&gt;signed&lt;/em&gt; or &lt;em&gt;unsigned&lt;/em&gt;, but by default, they're signed. If an integer is signed, it sacrifices its most significant bit to be the "sign bit." If the sign bit is 1, the number is negative; otherwise it's positive. An astute reader might realize that the change that happens here is in the range of possible numbers. Consider 8-bit numbers. There are 256 possible numbers to represent (given by 2^8). With an unsigned 8-bit integer, the values 0–255 can be represented; with a signed 8-bit int, -128–127 can be represented.&lt;/p&gt;

&lt;p&gt;To get the inverse of a binary number, use two's compliment. Let's find -5 in binary.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start with 5. In binary, 5 is &lt;code&gt;0101&lt;/code&gt;. The leading 0 is the sign.&lt;/li&gt;
&lt;li&gt;Invert each bit. &lt;code&gt;0101&lt;/code&gt; → &lt;code&gt;1010&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add 1 to this number (ignoring any possible overflow). &lt;code&gt;1010 + 0001 = 1011&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Your Turn!
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Confirm that -5 is &lt;code&gt;1011&lt;/code&gt; in binary by performing two's compliment on it to get 5, or &lt;code&gt;0101&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Write a C program that prints the size of an int in both bytes and bits. Use the code above as a starting point. &lt;em&gt;Hint: to convert from bytes to bits, how many bits are in a byte?&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Fill in the following table with sizes of different types, modifying your program to check them.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Size (bytes)&lt;/th&gt;
&lt;th&gt;Size (bits)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;int64_t&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;int8_t&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;char&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bool (you'll need to &lt;code&gt;#include &amp;lt;stdbool.h&amp;gt;&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;long int&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;short int&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;long long int&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;double&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;double&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Sample Responses
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Question 1
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Start with -5: &lt;code&gt;1011&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Invert each bit: &lt;code&gt;1011&lt;/code&gt; → &lt;code&gt;0100&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add 1: &lt;code&gt;0100 + 0001 = 0101&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Question 2
&lt;/h3&gt;

&lt;p&gt;Here's an example of what your simple program might look like (you can also check it out at &lt;code&gt;Chapter 1/SizeOfOperatorTest.c&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt; &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
 &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The size of an int is %lu bytes, or %lu bits.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go ahead and compile it using &lt;code&gt;gcc&lt;/code&gt; and check out its output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;Chapter&lt;span class="se"&gt;\ &lt;/span&gt;1
gcc &lt;span class="nt"&gt;-o&lt;/span&gt; sizeof SizeOfOperatorTest.c
./sizeof
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Question 3
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Size (bytes)&lt;/th&gt;
&lt;th&gt;Size (bits)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;int64_t&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;int8_t&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;char&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bool (you'll need to &lt;code&gt;#include &amp;lt;stdbool.h&amp;gt;&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;long int&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;short int&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;long long int&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;double&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;double&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Take &lt;em&gt;This&lt;/em&gt; Home
&lt;/h3&gt;

&lt;p&gt;The main point I'd like you to keep in mind is that &lt;strong&gt;with control of every bit, we can optimize our memory usage&lt;/strong&gt;. Though that has little effect on modern systems, in the case of embedded computing, every byte counts. &lt;strong&gt;By manually reading and writing bits as opposed to typical variable values, we can harness more functionality from less storage.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Chapter 2: Operating on Bits
&lt;/h2&gt;

&lt;p&gt;Now that we've covered data types and how data is stored, we're ready to introduce the idea of bitwise operations. Put simply, a &lt;strong&gt;bitwise operation&lt;/strong&gt; is an operation done on each bit of a piece of data. Let's take a look at each bitwise operator. Then, we'll implement them in C.&lt;/p&gt;

&lt;h3&gt;
  
  
  And (&amp;amp;)
&lt;/h3&gt;

&lt;p&gt;Written &lt;code&gt;x &amp;amp; y&lt;/code&gt; in C. This operator takes the corresponding bits of each number and performs an and operation. An and operation returns 1 (true) &lt;em&gt;if and only if both bits are 1&lt;/em&gt;. This means that two bits that are both 0 do &lt;strong&gt;not&lt;/strong&gt; return 1—they return 0. The result is the number made up of the binary string of results from each and. It's easiest to see this in a truth table. &lt;/p&gt;

&lt;p&gt;Consider the operation &lt;code&gt;int result = 3 &amp;amp; 5&lt;/code&gt;. First, convert 3 and 5 to binary.&lt;br&gt;
Now, we have &lt;code&gt;int result = 011 &amp;amp; 101&lt;/code&gt;. Consider the following truth table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;AND&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The result of the and operation is &lt;code&gt;001&lt;/code&gt;, which when converted to decimal is 1.&lt;/p&gt;
&lt;h3&gt;
  
  
  Or (|)
&lt;/h3&gt;

&lt;p&gt;Written &lt;code&gt;x | y&lt;/code&gt; in C. This operator takes the corresponding bits of each number and performs an or operation. An or operation returns 1 &lt;em&gt;if either bit is 1&lt;/em&gt;. As with other bitwise operators, the result is the number made up of the binary string of results from each or.&lt;/p&gt;

&lt;p&gt;Consider the operation &lt;code&gt;int result = 3 | 5&lt;/code&gt;. First, convert 3 and 5 to binary.&lt;br&gt;
Now, we have &lt;code&gt;int result = 011 | 101&lt;/code&gt;. Consider the following truth table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;OR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The result of the or operation is &lt;code&gt;111&lt;/code&gt;, which when converted to decimal is 7.&lt;/p&gt;
&lt;h3&gt;
  
  
  Xor (^)
&lt;/h3&gt;

&lt;p&gt;Written &lt;code&gt;x ^ y&lt;/code&gt; in C. This operator takes the corresponding bits of each number and performs an xor (exclusive or) operation. An xor operation returns 1 &lt;em&gt;if and only if one of the two bits is 1&lt;/em&gt;. As with other bitwise operators, the result is the number made up of the binary string of results from each or.&lt;/p&gt;

&lt;p&gt;Consider the operation &lt;code&gt;int result = 3 ^ 5&lt;/code&gt;. First, convert 3 and 5 to binary.&lt;br&gt;
Now, we have &lt;code&gt;int result = 011 ^ 101&lt;/code&gt;. Consider the following truth table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;XOR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The result of the xor operation is &lt;code&gt;110&lt;/code&gt;, which when converted to decimal is 6.&lt;/p&gt;
&lt;h3&gt;
  
  
  Left Shift (&amp;lt;&amp;lt;)
&lt;/h3&gt;

&lt;p&gt;Written &lt;code&gt;x &amp;lt;&amp;lt; amount&lt;/code&gt; Unlike the above operators, this operator only operates on one number. Each bit in the given number is shifted to the left by the given amount. Any bits that reach the end of the number are truncated (and zeros appear on the right side). Let's walk through an example.&lt;/p&gt;

&lt;p&gt;Consider &lt;code&gt;int result = 5 &amp;lt;&amp;lt; 2&lt;/code&gt;. As you know, 5 is &lt;code&gt;101&lt;/code&gt; in binary. Let's walk through each iteration of the shift.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initial&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;After one shift&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The binary result is &lt;code&gt;100&lt;/code&gt;, which is 4 in decimal.&lt;/p&gt;
&lt;h3&gt;
  
  
  Right Shift (&amp;gt;&amp;gt;)
&lt;/h3&gt;

&lt;p&gt;Written &lt;code&gt;x &amp;gt;&amp;gt; amount&lt;/code&gt; This operator is just like the left shift, except it works in the opposite direction. Each bit in the given number is shifted to the right by the given amount. Any bits that reach the end of the number are truncated (and zeros appear on the left side). Let's walk through an example.&lt;/p&gt;

&lt;p&gt;Consider &lt;code&gt;int result = 3 &amp;gt;&amp;gt; 2&lt;/code&gt;. As you know, 3 is &lt;code&gt;011&lt;/code&gt; in binary. Let's walk through each iteration of the shift.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initial&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;After one shift&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The binary result is &lt;code&gt;000&lt;/code&gt;, which is 0 in decimal.&lt;/p&gt;
&lt;h3&gt;
  
  
  Not (~)
&lt;/h3&gt;

&lt;p&gt;Written &lt;code&gt;~x&lt;/code&gt;. The not operator inverts all the bits of the given number. Once again, we'll use a truth table to elaborate.&lt;/p&gt;

&lt;p&gt;Consider &lt;code&gt;int result = ~5&lt;/code&gt;. As you know, 5 is &lt;code&gt;101&lt;/code&gt; in binary.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;~ Bit A&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Hence, the result of the not operation is &lt;code&gt;010&lt;/code&gt;, which is 2 in binary.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Left Shift &amp;amp; Right Shift Restrictions
&lt;/h4&gt;

&lt;p&gt;There are a few notable restrictions placed on these shift operations. For starters, you cannot shift bits a negative number of times—that just doesn't make sense! Also, shifting for more than the number of bits allocated to your variable is considered &lt;em&gt;undefined&lt;/em&gt;. You &lt;em&gt;can&lt;/em&gt; do it, but its output is not guaranteed to be constant for a given value. Finally, although not a restriction per-say, shifting 0 times simply doesn't perform a shift.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Your Turn!
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Complete a truth table for each of the following. Consider all values to be unsigned. Convert to decimal when complete.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;8 &amp;amp; 2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;6 | 3&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;7 ^ 5&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(5 &amp;amp; 2) &amp;amp; (4 &amp;amp; 3)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(5 | 2) &amp;amp; (4 &amp;amp; 3)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;(5 &amp;amp; 2) ^ (4 | 3)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Complete each operation. Consider all values to be unsigned and as long as the longest value in the problem needs to be (i.e., if you have the largest value of 8, deal with 4 bits). Convert to decimal when complete.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;~6&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;9 &amp;lt;&amp;lt; 4&lt;/code&gt; (here consider the value to be of length 32, so there's room to left shift).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;~(7 &amp;amp; 8)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;(2 | 6) &amp;gt;&amp;gt; 1&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;8 &amp;gt;&amp;gt; (~2)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;~((3 &amp;gt;&amp;gt; 2) ^ ~7) &amp;amp; (~(7 &amp;gt;&amp;gt; 4) | 2)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Sample Responses
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Question 1
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;8 &amp;amp; 2&lt;/code&gt; → &lt;code&gt;1000 &amp;amp; 0010&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;AND&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;⇒ &lt;code&gt;0000&lt;/code&gt;, which is 0 in decimal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;6 | 3&lt;/code&gt; → &lt;code&gt;110 | 011&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;OR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;⇒ &lt;code&gt;111&lt;/code&gt;, which is 7 in decimal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;7 ^ 5&lt;/code&gt; → &lt;code&gt;111 ^ 101&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;XOR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;⇒ &lt;code&gt;010&lt;/code&gt;, which is 2 in decimal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(5 &amp;amp; 2) &amp;amp; (4 &amp;amp; 3)&lt;/code&gt; → &lt;code&gt;(101 &amp;amp; 010) &amp;amp; (100 &amp;amp; 011)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;A AND B&lt;/th&gt;
&lt;th&gt;Bit C&lt;/th&gt;
&lt;th&gt;Bit D&lt;/th&gt;
&lt;th&gt;C AND D&lt;/th&gt;
&lt;th&gt;(A AND B) AND (C AND D)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;⇒ &lt;code&gt;000&lt;/code&gt;, which is 0 in decimal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(5 | 2) &amp;amp; (4 &amp;amp; 3)&lt;/code&gt; → &lt;code&gt;(101 | 010) &amp;amp; (100 &amp;amp; 011)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;A OR B&lt;/th&gt;
&lt;th&gt;Bit C&lt;/th&gt;
&lt;th&gt;Bit D&lt;/th&gt;
&lt;th&gt;C AND D&lt;/th&gt;
&lt;th&gt;(A OR B) AND (C AND D)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;⇒ &lt;code&gt;000&lt;/code&gt;, which is 0 in decimal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(5 &amp;amp; 2) ^ (4 | 3)&lt;/code&gt; → &lt;code&gt;(101 &amp;amp; 010) ^ (100 | 011)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Bit A&lt;/th&gt;
&lt;th&gt;Bit B&lt;/th&gt;
&lt;th&gt;A AND B&lt;/th&gt;
&lt;th&gt;Bit C&lt;/th&gt;
&lt;th&gt;Bit D&lt;/th&gt;
&lt;th&gt;C OR D&lt;/th&gt;
&lt;th&gt;(A AND B) XOR (C OR D)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;⇒ &lt;code&gt;111&lt;/code&gt;, which is 7 in decimal.&lt;/p&gt;
&lt;h3&gt;
  
  
  Question 2
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;~6&lt;/code&gt; → &lt;code&gt;~110&lt;/code&gt; ⇒ &lt;code&gt;011&lt;/code&gt;, which is 3 in decimal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;9 &amp;lt;&amp;lt; 4&lt;/code&gt; → &lt;code&gt;001001 &amp;lt;&amp;lt; 4&lt;/code&gt; ⇒ &lt;code&gt;100100&lt;/code&gt;, which is 36 in decimal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;~(7 &amp;amp; 8)&lt;/code&gt; → &lt;code&gt;~(0111 &amp;amp; 1000)&lt;/code&gt; → &lt;code&gt;~(0000)&lt;/code&gt; ⇒ &lt;code&gt;1111&lt;/code&gt;, which is 15 in decimal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;(2 | 6) &amp;gt;&amp;gt; 1&lt;/code&gt; → &lt;code&gt;(010 | 110) &amp;gt;&amp;gt; 1&lt;/code&gt; → &lt;code&gt;110 &amp;gt;&amp;gt; 1&lt;/code&gt; ⇒ &lt;code&gt;011&lt;/code&gt;, which is 3 in decimal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;8 &amp;gt;&amp;gt; (~2)&lt;/code&gt; → &lt;code&gt;1000 &amp;gt;&amp;gt; ~(10)&lt;/code&gt; → &lt;code&gt;1000 &amp;gt;&amp;gt; (01)&lt;/code&gt; → &lt;code&gt;1000 &amp;gt;&amp;gt; 1&lt;/code&gt; ⇒ &lt;code&gt;0100&lt;/code&gt;, which is 4 in decimal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;~((3 &amp;gt;&amp;gt; 2) ^ ~7) &amp;amp; (~(7 &amp;gt;&amp;gt; 4) | 2)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To solve this, I suggest splitting into halves:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;~((3 &amp;gt;&amp;gt; 2) ^ ~7)&lt;/code&gt; and &lt;code&gt;(~(7 &amp;gt;&amp;gt; 4) | 2)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;~((3 &amp;gt;&amp;gt; 2) ^ ~7)&lt;/code&gt; → &lt;code&gt;~((011 &amp;gt;&amp;gt; 2) ^ ~(111))&lt;/code&gt; → &lt;code&gt;~((000) ^ ~(111))&lt;/code&gt; → &lt;code&gt;~(000 ^ 000)&lt;/code&gt; → &lt;code&gt;111&lt;/code&gt;&lt;br&gt;
&lt;code&gt;(~(7 &amp;gt;&amp;gt; 4) | 2)&lt;/code&gt; → &lt;code&gt;(~(111 &amp;gt;&amp;gt; 4) | 010)&lt;/code&gt; → &lt;code&gt;(~(000) | 010)&lt;/code&gt; → &lt;code&gt;(111 | 010)&lt;/code&gt; → &lt;code&gt;111&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Hence, &lt;code&gt;111 &amp;amp; 111&lt;/code&gt; ⇒ &lt;code&gt;111&lt;/code&gt;, which is 7 in decimal.&lt;/p&gt;
&lt;h2&gt;
  
  
  Chapter 3: Applying Bitwise Operations in C
&lt;/h2&gt;

&lt;p&gt;This chapter is all about writing C code that utilizes bitwise operators. Before we get to doing bitwise operations, we should begin by writing a function that can write the binary equivalent of a given variable.&lt;/p&gt;

&lt;p&gt;To do this, we use a mask. We initialize it to contain a &lt;code&gt;1&lt;/code&gt; in the most significant (leftmost in little-endian systems) bit followed by zeros. With each iteration of a loop, we right shift the mask by 1, moving the 1 all the way "across" the mask. When we use the &lt;code&gt;&amp;amp;&lt;/code&gt; operator on the pointer and the mask, any non-zero value means that a &lt;code&gt;1&lt;/code&gt; occurred somewhere in the result. Since there's only one &lt;code&gt;1&lt;/code&gt; in the mask, we know exactly where this occurred. Since the loop moves from left to right, we can just append the corresponding bit's character to the string. The string is one character longer than the size because it needs to contain the null character (&lt;code&gt;\0&lt;/code&gt;). This is how &lt;code&gt;printf&lt;/code&gt; knows to stop printing, and omitting it can lead to numerous errors and/or unexpected behaviors (like the printing of other data from in memory).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printBinary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// To determine size (in bits), we multiply the maximum size of an unsigned int by 8 (to convert from bytes to bits).&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// This counts the leading zeros of the number, then subtracts them from the size.&lt;/span&gt;
    &lt;span class="c1"&gt;// Hence, we start at the first bit set to 1 (unless decimal == 0)&lt;/span&gt;
    &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;__builtin_clz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Handle zero case, we need one space to display "0."&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;binary&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;binary&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;binary&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;binary&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;binary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Bitwise Assignment Operators
&lt;/h3&gt;

&lt;p&gt;All bitwise operators, except for not (&lt;code&gt;~&lt;/code&gt;), can be used in the assignment fashion. This means you can add an equals sign right next to one of the bitwise operator. For example, in&lt;/p&gt;


&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;&lt;code&gt;a&lt;/code&gt; is both the first operand and the destination. In other words, the value of a &amp;amp; 7 is stored in a. This works for all bitwise operators aside from the not (&lt;code&gt;~&lt;/code&gt;) operator.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, I'd like to provide a few case study like prompts for you to try. Sample responses are available.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case Study 1: Unix File Permissions
&lt;/h3&gt;

&lt;p&gt;One use case of bitwise operations is in the Unix permission system. You've probably run the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;777 some-file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what do each of the numbers mean? And why 7? The reality is, binary is at play here, and 7 should tip you off. Recall that 7 in binary is &lt;code&gt;111&lt;/code&gt;. The number being passed here is acting as three flags or booleans glued together.&lt;/p&gt;

&lt;p&gt;The three digits specified are for three classes of users:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The file owner;&lt;/li&gt;
&lt;li&gt;Group members of the file owner;&lt;/li&gt;
&lt;li&gt;and everyone else.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As I mentioned above, each digit is a conglomeration of three flags, each representing a permission. The flag (binary bit) in the fours place represents read permission, the twos place is for write permission, and the ones is for execute. So,&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;chmod 777 some-file&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 is doing this under the hood:&lt;/p&gt;

&lt;h4&gt;
  
  
  File Permissions: &lt;code&gt;some-file&lt;/code&gt;
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Group&lt;/th&gt;
&lt;th&gt;Read&lt;/th&gt;
&lt;th&gt;Write&lt;/th&gt;
&lt;th&gt;Execute&lt;/th&gt;
&lt;th&gt;Decimal&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Owner&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Owner's Group&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Everyone Else&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In other words, all permissions are given to all.&lt;/p&gt;

&lt;h4&gt;
  
  
  Task
&lt;/h4&gt;

&lt;p&gt;Design a simple permissions checker that takes in a full file permission value (a three-digit number) and checks for a given set of specific permissions (i.e., owner write, everyone execute, etc.). For an example, check the &lt;code&gt;Chapter 3&lt;/code&gt; folder.&lt;/p&gt;

&lt;h4&gt;
  
  
  Hint
&lt;/h4&gt;

&lt;p&gt;After taking in a full number, you'll need to convert it to an &lt;code&gt;int&lt;/code&gt; (from a &lt;code&gt;char*&lt;/code&gt;). Then, use modular arithmetic to break down each permission set. Remember, the first digit represents the owner's permissions, the second is for the owner's user group, and the third is for everyone.&lt;/p&gt;

&lt;p&gt;To check if a specific permission occurs in a permission set, bitwise and the given permission with the set.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 2: Subnetting a Network
&lt;/h3&gt;

&lt;p&gt;If you've ever configured a router, you may have noticed an option to configure the "subnet mask." Usually, this is set to &lt;code&gt;255.255.255.0&lt;/code&gt;, but why? Firstly, we need to remember that each byte of an IPv4 address is separated by a '.'. When dealing with the type of network you're most familiar with (a class C network), there are 3 bytes dedicated to the network and the final byte is for the host. &lt;/p&gt;

&lt;p&gt;Being that the subnet mask is a mask, you might be catching on to its purpose. For each bit you "borrow" from the host byte, two subnets are created.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  Network Address
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;network address&lt;/strong&gt; has all &lt;em&gt;host&lt;/em&gt; bits set to &lt;code&gt;0&lt;/code&gt;. This means any bit surrendered to create&lt;br&gt;
a subnet still could be set to &lt;code&gt;1&lt;/code&gt;. &lt;/p&gt;
&lt;h4&gt;
  
  
  Read More!
&lt;/h4&gt;

&lt;p&gt;Learn more about subnets by checking out &lt;a href="https://networklessons.com/subnetting/subnetting-in-binary" rel="noopener noreferrer"&gt;this&lt;/a&gt; website.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Task
&lt;/h4&gt;

&lt;p&gt;In C, write a program that takes in an IPv4 address and a subnet mask and finds and outputs the network address that the IPv4 address lives in. For an example, check the &lt;code&gt;Chapter 3&lt;/code&gt; folder.&lt;/p&gt;

&lt;h4&gt;
  
  
  Hint
&lt;/h4&gt;

&lt;p&gt;You'll need to store each byte of the address and mask as a numerical value. To find the network address, consider which (bitwise) operation between the mask and address will create the intended effect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;I hope this explainer was useful for you! I wrote it because I wanted to learn about bitwise operations myself. I've checked it, but some things could be wrong, so feel free to correct me via a pull request, or even add a comment! Also, if you've got any questions, leave a comment! I can't wait to chat with you! To close, I'm so happy to have been able to provide this resource for you!&lt;/p&gt;

&lt;h2&gt;
  
  
  About Me
&lt;/h2&gt;

&lt;p&gt;Hi! I'm Jackson, a student of computer science &amp;amp; French at Lafayette College and an aspiring researcher and professor in computer science. I'm currently interested in the fields of bioinformatics and low-level programming/systems. To learn more about me, check out my &lt;a href="https://jacksoneshbaugh.github.io" rel="noopener noreferrer"&gt;site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>c</category>
      <category>assembly</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
