<?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>DEV Community: Max Feige</title>
    <description>The latest articles on DEV Community by Max Feige (@max_feige).</description>
    <link>https://dev.to/max_feige</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%2F1082481%2Fce3c8a2e-4729-4c9a-acc2-1368d7389323.png</url>
      <title>DEV Community: Max Feige</title>
      <link>https://dev.to/max_feige</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/max_feige"/>
    <language>en</language>
    <item>
      <title>TypeScript 102 -  Beyond The Basics</title>
      <dc:creator>Max Feige</dc:creator>
      <pubDate>Wed, 23 Aug 2023 12:47:41 +0000</pubDate>
      <link>https://dev.to/max_feige/typescript-102-beyond-the-basics-3eh5</link>
      <guid>https://dev.to/max_feige/typescript-102-beyond-the-basics-3eh5</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KuFkvYzi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxltaqum8jx6qaoqp4l0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KuFkvYzi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxltaqum8jx6qaoqp4l0.png" alt="Beyond The Basics Image" width="700" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The seemingly endless sea of TypeScript tutorials you'll find online do a great job of providing resources to both beginners and advanced users, but what about those in the middle? If you know the basics but aren't quite at the advanced level yet, then this series of articles is for you. In it we'll be exploring some tools and techniques that'll take your TypeScript code to the next level!&lt;/p&gt;

&lt;p&gt;This guide assumes that you're already familiar with the basics of TypeScript and JavaScript. You should understand the concepts of types, interfaces, the &lt;code&gt;as&lt;/code&gt; keyword, functions, and classes, as we'll be building on all of these fundamentals.&lt;/p&gt;

&lt;p&gt;Let's start in the shallow end: the building blocks TypeScript gives us and what we can do with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Union (|) and Intersection (&amp;amp;) Types
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YZsnn3AA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51orjdx7nynogtulr7tm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YZsnn3AA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51orjdx7nynogtulr7tm.png" alt="Union and Intersection Venn Diagram" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unions and intersection types are incredibly useful for enhancing the flexibility of your code.&lt;/p&gt;

&lt;p&gt;A union type, denoted by &lt;code&gt;|&lt;/code&gt;, allows a variable to be one of several types. This is useful when we want our code to accommodate a variety of data types. Here's an example using interfaces:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this case, a person is either a Teacher or a Coder. The &lt;code&gt;work&lt;/code&gt; method, which is shared by Teacher and Coder, is valid since it's present regardless of which type person is. Methods specific to one interface, however, may not exist in person, and that's where TypeScript steps in to stop us from trying to use them. Without additional work, TypeScript will only allow us to use properties that it knows will exist.&lt;/p&gt;

&lt;p&gt;We can also use unions with primitive types:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this example, we're allowing &lt;code&gt;myVar&lt;/code&gt; to be a &lt;code&gt;number&lt;/code&gt; or a &lt;code&gt;string&lt;/code&gt;. TypeScript ensures we can only use properties or methods common to all types in the union, thus preventing us from using the string-only method &lt;code&gt;toLowerCase&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When we use a union, we're establishing that any variable assigned to our type is going to satisfy the properties of at least one of the provided types. TypeScript will then go further and only allow us to access properties and methods common to all types - at least if we don't give TypeScript more information to narrow the type with! Don't worry if that sounds complicated, we'll dive into narrowing shortly.&lt;/p&gt;

&lt;p&gt;Next up is the intersection! Intersections, denoted by &lt;code&gt;&amp;amp;&lt;/code&gt;, complement the union and allow us to merge multiple types into one. A variable declared using an intersection must have the features of all combined types. Let's illustrate this with an example:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, an intersection allows us to create a new type by combining the overlapping properties of both interfaces. Intersection types can be used with primitives as well, as shown here:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Since the primitive types &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;number&lt;/code&gt; are mutually exclusive, an intersection is impossible. The intersection results in the type &lt;code&gt;never&lt;/code&gt;, which we'll talk more about soon. For now, just know that it symbolizes a type that can't exist.&lt;/p&gt;

&lt;p&gt;If we try to intersect two types with conflicting properties we're also greeted by &lt;code&gt;never&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this example intersection, &lt;code&gt;id&lt;/code&gt; would be required to be both number and string simultaneously, which is impossible. TypeScript sees this and assigns it the type &lt;code&gt;never&lt;/code&gt;, effectively preventing us from using our newly created type.&lt;/p&gt;

&lt;p&gt;Overall, intersections allow us to construct complex types from simple building blocks, and TypeScript will ensure that any values we assign to an intersection satisfy all types in the intersection.&lt;/p&gt;

&lt;p&gt;Utilizing both unions and intersections effectively gives you the ability to easily mix and match your existing types with ease, saving you the effort of manually creating similar types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type Narrowing
&lt;/h2&gt;

&lt;p&gt;One of TypeScript's most important features is type narrowing, a process by which the type of a variable is (wait for it) narrowed using, for example, a conditional. This gives TypeScript more information about what type we're providing it. Let's expand on a previous example to see this in action:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Our function &lt;code&gt;specialPrint&lt;/code&gt; takes an item, which is either a string or a number. The function will then check if item is a string and will use either &lt;code&gt;toLowerCase&lt;/code&gt; or &lt;code&gt;toFixed&lt;/code&gt; based on the result. Thanks to the conditionals the methods are contained in, TypeScript will recognize that the code can only reach these methods when item is the correct type and will bypass its limitations on methods that aren't shared between every type in the union.&lt;/p&gt;

&lt;p&gt;Ever have a variable in limbo waiting for user input or for a network request to finish? These variables normally start out as undefined, which can be a common source of headaches for many JavaScript developers. TypeScript gives us the tools to tackle this issue by using a combination of unions and type narrowing. This approach helps us write code that's clean and bug-free!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this example, any Person could be a &lt;code&gt;Teacher&lt;/code&gt;, &lt;code&gt;Coder&lt;/code&gt;, or &lt;code&gt;null&lt;/code&gt;. Since &lt;code&gt;null&lt;/code&gt; has no methods, we cannot directly call &lt;code&gt;person.work()&lt;/code&gt;. Instead, we must ensure that person is not &lt;code&gt;null&lt;/code&gt; with a simple conditional. Once we've done this, TypeScript recognizes that person is type &lt;code&gt;Teacher | Coder&lt;/code&gt; and we're able to call the &lt;code&gt;work&lt;/code&gt; method safely.&lt;/p&gt;

&lt;p&gt;This is where the power of type narrowing really shines. By using type checks, we can ensure that our code behaves as expected and is free of runtime errors. This makes our code safer, easier to understand, and easier to maintain. There are many more ways we can employ type narrowing in TypeScript, but that's an article for another day. For now, just know that TypeScript has an eye on your conditional statements and will understand which types are possible within them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Any, Unknown, and Never
&lt;/h2&gt;

&lt;p&gt;TypeScript has a few special types: &lt;code&gt;any&lt;/code&gt;, &lt;code&gt;unknown&lt;/code&gt;, and &lt;code&gt;never&lt;/code&gt;. Let's start by taking a look at &lt;code&gt;any&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Thanks to &lt;code&gt;any&lt;/code&gt;, the above code is perfectly acceptable and will not yield any errors! A variable of type any has the flexibility to be set to whatever value you want, and other variables of different types can be set to it without issue. In other words, TypeScript does no type-checking on a variable of type &lt;code&gt;any&lt;/code&gt;. While you might occasionally want a variable to be treated like this, type-checking is one of the best reasons to use TypeScript in the first place. This means that it's usually best to use &lt;code&gt;unknown&lt;/code&gt; instead.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Although you can set an unknown variable to whatever value you want, other variables cannot be directly set to it. Instead you must explicitly cast its type, like so:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Using &lt;code&gt;unknown&lt;/code&gt; is preferred to using any because it encourages us to explicitly pick when and how we convert it to another type.&lt;br&gt;
Finally, let's look at &lt;code&gt;never&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;never&lt;/code&gt; type is restrictive in that it prevents the assignment of any values to it. Similarly, no other variables can be set to a variable of type &lt;code&gt;never&lt;/code&gt;. This type symbolizes an impossibility; something that should &lt;em&gt;never&lt;/em&gt; happen. One common place we may see never is in functions that should always throw errors and &lt;em&gt;never&lt;/em&gt; return a value:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;I've made a short summary chart for these three types:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MuhidE0B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnqzimvudd9dpbpft82x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MuhidE0B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnqzimvudd9dpbpft82x.png" alt="Recommended usage of any,unknown,never chart" width="491" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All three of these serve distinct purposes, each with different balances between type safety and flexibility. While &lt;code&gt;any&lt;/code&gt; provides maximum flexibility, it comes at the cost of losing the type safety benefits given by TypeScript. Because of this, you should instead use the &lt;code&gt;unknown&lt;/code&gt; type and explicitly tell TypeScript to cast your variables. Lastly, the &lt;code&gt;never&lt;/code&gt; type will always give us an error when we try to interact with it. We use &lt;code&gt;never&lt;/code&gt; when we want to tell TypeScript that there should be no scenario in which an outcome occurs, and that you want an error to be thrown if it does. We'll go deeper into some advanced uses of never in future articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Literal Types
&lt;/h2&gt;

&lt;p&gt;TypeScript also gives us literals, another powerful feature that represents exact values and can be a specific instance of a &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, or &lt;code&gt;boolean&lt;/code&gt;. Let's look at one:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here we created a variable called userRole with the type "basic". Because of this, the variable myUserRole can no longer be assigned to any other string. A literal on its own doesn't seem that useful, but when combined with some of the other functionalities we've explored we can construct some useful types:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Through a combination of unions and literals, we've created a type that could have one of three possible values. This could come in handy!&lt;/p&gt;

&lt;p&gt;When we create a variable with &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt;, TypeScript typically assigns it a general type (like &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, or &lt;code&gt;boolean&lt;/code&gt;). Alternatively, if we declare a variable with &lt;code&gt;const&lt;/code&gt;, TypeScript will assume that its type is a literal. Let's check out an example of this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We can also tell Typescript to behave this way when declaring with let or var by using as const.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;as const&lt;/code&gt; is a helpful way to make sure that your types are as specific as possible. Additionally, we can use &lt;code&gt;as const&lt;/code&gt; on objects to ensure that the fields are specific:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Overall, literals are an excellent way to limit the values our variables can contain. You can see that &lt;code&gt;as const&lt;/code&gt; has changed the type of our object's fields into literals. It's also added the keyword &lt;code&gt;readonly&lt;/code&gt;, though. What's happening here?&lt;/p&gt;

&lt;h2&gt;
  
  
  Readonly
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vFuf3ISA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbx4pr5ldnypn2ccj6qz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vFuf3ISA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbx4pr5ldnypn2ccj6qz.png" alt="No writing" width="256" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TypeScript introduces several new keywords, one of which is &lt;code&gt;readonly&lt;/code&gt;. This keyword allows us to specify parts of an object as mostly unchangeable. Let's investigate:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now TypeScript will give us an error whenever we try to change (or "write to") the id variable, hence the name &lt;code&gt;readonly&lt;/code&gt;. The only time a &lt;code&gt;readonly&lt;/code&gt; property can be set is during initialization, or when the object is created. This concept also applies to classes:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Just like with our interface example, we can't change the id field after it's been initialized. For a class, this means we can only change id in the constructor. After the constructor has finished executing, TypeScript will no longer allow us to change that field.&lt;/p&gt;

&lt;p&gt;Finally, readonly can be applied to arrays:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This prevents us from adding, removing, or changing any element in our array, but we can still assign a new array to the variable containing it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;readonly&lt;/code&gt; is an excellent keyword to use in large codebases, as it allows you to explicitly choose when something should be observable but unchangeable. This bars people from accidentally modifying data they shouldn't be able to, which in turn prevents bugs and clarifies the intent of your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The features explained in this article are among the most frequently used in TypeScript, and by thoughtfully utilizing them yourself you can construct less error-prone systems and maintain JavaScript's inherent flexibility. Remember, TypeScript isn't about making your life harder! It's about making your code safer, more maintainable, and easier to understand.&lt;br&gt;
As you continue to use and explore TypeScript, I encourage you to try out these concepts and discover for yourself how you can apply them to your own projects. In the next article, we'll dive deeper into some of the common utility types given by TypeScript. Stay tuned!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Bit Manipulation: Exploring A Lost Technique of Programming</title>
      <dc:creator>Max Feige</dc:creator>
      <pubDate>Mon, 31 Jul 2023 12:20:33 +0000</pubDate>
      <link>https://dev.to/max_feige/bit-manipulation-exploring-a-lost-technique-of-programming-4dlh</link>
      <guid>https://dev.to/max_feige/bit-manipulation-exploring-a-lost-technique-of-programming-4dlh</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o94fmlZh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvzv928323tibnuuxxfo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o94fmlZh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvzv928323tibnuuxxfo.png" alt="SICP Bitwise Parody Image" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In an era when processors were sluggish and memory scarce, programmers had to come up with ingenious little tricks to accelerate their software, the king of which was bit manipulation. Yet as technology has progressed, our faster processors and sophisticated programming languages have relegated these hacks to obscurity. Though they no longer offer the same tangible performance advantages, rediscovering them can enrich our perception of how logic and creativity exist in the world of coding.&lt;/p&gt;

&lt;p&gt;We're set to take a deep dive into some of my favorite bit hacks, exploring and understanding their underlying mechanics. As we delve into the intricacies of each trick my hope is that you'll gain a richer understanding of what's happening beneath the surface of your code. At minimum I hope to install in you an admiration for the cleverness encapsulated in these hacks.&lt;br&gt;
First lets take a quick review on how binary numbers work:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VLd9by3Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szwfz8wkxbavyzvg0p4e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VLd9by3Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szwfz8wkxbavyzvg0p4e.png" alt="Image description" width="800" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll talk about the specific bitwise operations we use for each problem, but feel free to reference this table or check the &lt;a href="https://en.wikipedia.org/wiki/Bitwise_operation"&gt;Wikipedia page&lt;/a&gt; page for more details:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SjLc1sn---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vq3jthx8rythxl91xf2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SjLc1sn---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vq3jthx8rythxl91xf2.png" alt="Image description" width="619" height="151"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Example Problems
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Even Or Odd?
&lt;/h2&gt;

&lt;p&gt;Every coder's journey starts somewhere, and often one of the first stops is the basic math problem: "Is this number even or odd?" It's a rite of passage, teaching some foundational concepts, providing some quick feedback, and being just fun enough to be engaging.&lt;br&gt;
First the classic approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;isOdd&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;number&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;number&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&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="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&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;In this version, we're checking if the number is divisible by two (the textbook definition of evenness) - no remainder, no oddness. But let's check out how a bit based solution works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;isOdd&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;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that's an elegant solution! No comparison, division/modulo, or jump required. A simple AND that outperforms the conventional method with ease.&lt;/p&gt;

&lt;p&gt;What our code is doing bitwise is checking if the final bit in the number is on. In the same way that we can know a number is odd in base 10 by considering the final digit (checking if it is 1,3,5,7, or 9), we can do so in binary - but here we only have two digits to consider. If the digit is 1 then we know it to be odd, otherwise it is even. This AND statement gives us the final bit, which yields us a 1 in the case the number is odd, and a 0 otherwise. Conveniently, most languages will implicitly convert those to true and false respectively. As an exercise, you can verify this solution works even using negative numbers in &lt;a href="https://en.wikipedia.org/wiki/Two's_complement"&gt;two's complement&lt;/a&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Determining The Sign of a Number
&lt;/h2&gt;

&lt;p&gt;The challenge of identifying a number's sign is as ancient as it is pervasive. We often need to handle positive and negative numbers differently, requiring us to discern the sign of a given number. Let's first look at a simple modern approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sign&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="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;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="k"&gt;return&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="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&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;x&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="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;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a standard solution that's quite simple to arrive at. It's also quite long and has some if statements. Next let's look at the fastest bit based version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sign&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="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;31&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;h6&gt;
  
  
  Introducing The Right Shift Operator (&amp;gt;&amp;gt;): A shift operator is one the most simplistic operations our computer can do. It simply takes all of the bits in a number and moves them to the left. An arithmetic shift (the kind we use) also copies the neighbor bit when we add new digits. Let's look at an example:
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;00101010 &amp;gt;&amp;gt; 1  = 00010101 - move all of our bits to the right one space
10110101 &amp;gt;&amp;gt; 3  = 11110110 - move all of our bits to the left three spaces - note because a 1 was in the leftmost spot our 3 new digits to the left are also 1.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remarkably simple isn't it? This solution relies on certain conditions to work: it requires the int to be 32 bits, and for the most significant bit (left-most bit) to represent the sign of the number.  Given that most modern languages utilize the arithmetic shift, when a new bit is added to one side it replicates the adjacent bit.  Thus, if &lt;code&gt;x&lt;/code&gt; is negative, the resultant number will in binary be thirty-two 1s in binary, translating to &lt;code&gt;-1&lt;/code&gt; due to two's complement representation.  If &lt;code&gt;x&lt;/code&gt; is non-negative, then the leftmost bit is &lt;code&gt;0&lt;/code&gt; and all new bits given by the shift operation also become &lt;code&gt;0&lt;/code&gt;, leaving us with &lt;code&gt;0&lt;/code&gt;.  So, if &lt;code&gt;x&lt;/code&gt; is negative this code returns &lt;code&gt;-1&lt;/code&gt;; otherwise it returns &lt;code&gt;0&lt;/code&gt;.  As compact and efficient as this solution may be, it's important to note its underlying assumptions and that it doesn't return &lt;code&gt;1&lt;/code&gt; when the number is positive, unlike our initial solution.&lt;/p&gt;

&lt;p&gt;Can we find a compromise? A method that's quick and succinct, and aligns with the output of our original code? With a few tweaks to our existing code we can:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sign&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="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&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;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;31&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;h6&gt;
  
  
  Introducing The Or Operator (|) - The OR operator combines the bits of two numbers, producing a new number with a 1 in any position where either of the original numbers had a 1. Let's look at an example:
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;11001100 OR (|) 204
&lt;span class="gh"&gt;01010101         85
---------------------------
&lt;/span&gt;11011101        221
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's go over all three scenarios here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; is positive: Then the left side of our OR becomes &lt;code&gt;1&lt;/code&gt;, and the right side becomes &lt;code&gt;0&lt;/code&gt;, resulting in &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; is zero:  Then the left side becomes &lt;code&gt;0&lt;/code&gt;, and the right side becomes &lt;code&gt;0&lt;/code&gt;, resulting  &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; is negative: The left side becomes &lt;code&gt;1&lt;/code&gt;, and the right side becomes &lt;code&gt;-1&lt;/code&gt;, yielding &lt;code&gt;-1&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, like our previous solution, bear in mind this method hinges on the same set of assumptions about the language and hardware. While this approach might save us a few assembly instructions, it compromises platform-agnosticism. Therefore in the modern day or portable code, this solution is not advisable.&lt;/p&gt;

&lt;p&gt;We can achieve a platform-agnostic result by utilizing comparison operators, yielding the same results as the original method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sign&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="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;This solution does operate agnostically. I'll leave verification of this solution as an exercise to the reader - here's a hint: In most programming languages the boolean value &lt;code&gt;true&lt;/code&gt; is converted to &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; is converted to &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Swap Two Numbers
&lt;/h2&gt;

&lt;p&gt;Programmers love a good reshuffle! We may not always realize it, but every time we sort an array or a list we're engaged in a continuous process of swapping two variables. Let's recall the traditional way of swapping two numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;//setup&lt;/span&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;3&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&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;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or in the context an a list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&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;tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&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;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&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;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The typical swap sees us create a temporary variable to help a and b switch places.  But what if we could avoid using a temporary variable? With integers we can use a nifty bit trick to swap them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&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;3&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&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;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Introducing The XOR Operator (^) - The XOR operator combines the bits of two numbers, producing a new number with a 1 in any position where exactly one of the original numbers had a 1. Let's look at an example:
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;11001100 XOR (^)  204
&lt;span class="gh"&gt;01010101          85
---------------------------
&lt;/span&gt;10011001          153 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that looks quite cool! What's happening here is we are using the XOR operation to swap &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;. Lets break it down into a few different variables to see what's happening.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&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;3&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;r1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&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;r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&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;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&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;r3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt; &lt;span class="o"&gt;=&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;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&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;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does this simplify? Well the XOR operation has three useful properties we make use of: it's commutative &lt;code&gt;(A ^ B = B ^ A)&lt;/code&gt;, self-annihilating &lt;code&gt;(A ^ A = 0)&lt;/code&gt;, and has a zero identity &lt;code&gt;(0 ^ A = A)&lt;/code&gt;.  Using these properties we can simplify the above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&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;3&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;r1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&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;r2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&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;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&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;0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&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;r3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt; &lt;span class="o"&gt;=&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;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&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;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&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;^&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And just like that we've swapped a and b all without a temporary variable.  While this trick might not be useful in your everyday coding, it's a neat way to trade out an extra memory allocation for spending more CPU cycles.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;h2&gt;
  
  
  So why don't we use these?
&lt;/h2&gt;

&lt;p&gt;While these bit hacks are clever and may simplify the bytecode instructions, we rarely observe significant performance boosts implementing them today. This is primarily due to advancements in code compilers, and contemporary CPU features. Modern compilers are adept at uncovering and implementing these sorts of optimizations in your code, often without your explicit knowledge.&lt;/p&gt;

&lt;p&gt;Furthermore, the complexity of today's CPU capabilities - such as branch prediction, SIMD, out of order execution, and instruction level parallelism to name a few - renders the performance of a program far more intricate than simply associating a particular cycle count with each instruction type.&lt;/p&gt;

&lt;p&gt;Adding to their limited applicability is the fact that even if we wrote a function that managed to save 5 cycles each time its run, on a modern processor running at 3GHz, we'd need that function to execute several hundred thousand times to witness just a milliseconds worth of time savings. These aptly named micro-optimizations are rarely worth pursuing over macro level optimizations.&lt;/p&gt;

&lt;h2&gt;
  
  
  So when should we use these?
&lt;/h2&gt;

&lt;p&gt;These cunning bit manipulation tricks lend themselves well to a style of programming I call cheeky coding. Rather than focusing on writing quality code, cheeky coding emphasizes showcasing your skill in a dazzling, if somewhat mischievous, manner. The allure of bit tricks is their ability to obfuscate your code, irk your coworkers, and reduce its readability, all while maintaining a thin veneer of utility. The only time that I might recommend using this style would be in an interview setting, where it could impress your interviewer, or annoy them - ideally both.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>learning</category>
      <category>bitwise</category>
    </item>
    <item>
      <title>Exploring Gray Code – The Oddball Sibling of Binary</title>
      <dc:creator>Max Feige</dc:creator>
      <pubDate>Thu, 25 May 2023 13:45:00 +0000</pubDate>
      <link>https://dev.to/max_feige/exploring-gray-code-the-oddball-sibling-of-binary-2dg7</link>
      <guid>https://dev.to/max_feige/exploring-gray-code-the-oddball-sibling-of-binary-2dg7</guid>
      <description>&lt;p&gt;So hopefully we're all familiar with binary. Just in case, here's a review:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vwk7R_Yp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10krn3wyxova9de2m3nv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vwk7R_Yp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10krn3wyxova9de2m3nv.png" alt="Binary Counting" width="587" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's the standard way of counting in binary. And for computers, this is a perfectly logical and optimal system. Its the way most numbers on our computer work.&lt;a href="https://en.wikipedia.org/wiki/Two%27s_complement"&gt;*&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But what if we had to manually track the bits ourselves using, say, a series of heavy switches to store a counter, like counting people who enter a store? Let's take a look at our hypothetical counter:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wPL6MEa0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jekok68ba4m6kl7kc802.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wPL6MEa0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jekok68ba4m6kl7kc802.png" alt="Binary Switches 0" width="800" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After some period of time, we've had several people come in. So by now, we have 7 people: Here it is stored in binary with our counter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LQNShWqL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wi802ar5c3kji9xjvyh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LQNShWqL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wi802ar5c3kji9xjvyh.png" alt="Binary Counting 7" width="800" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Suddenly we hear the bell ding; someone else has entered the store. So we should increase the counter to eight:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--btQTVSA5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnz5o51hwp5vj42sdzjv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--btQTVSA5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnz5o51hwp5vj42sdzjv.png" alt="Binary Counting 8" width="800" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But man - that was hard! The effort required to flip multiple switches can quickly become overwhelming as the count increases. You might wonder if there's a more efficient method - and there is. It's called Gray Code.&lt;/p&gt;

&lt;p&gt;Gray Code is an alternative binary numeral system where two successive values differ in only one bit, which means flipping just one switch to go from one value to the next. Here's what counting from 0 to 7 looks like in Gray Code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gmx6oKO---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1egaubw1x0xlfop8z85v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gmx6oKO---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1egaubw1x0xlfop8z85v.png" alt="Gray Code 0 to 7" width="650" height="1950"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully you were able to see the pattern - each time we flip the switch furthest to the right that will give us a combination we haven't used. You can read more about Gray Code &lt;a href="https://en.wikipedia.org/wiki/Gray_code#Constructing_an_n-bit_Gray_code"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You might think that converting to and from Gray Code would be complicated - but thankfully some really smart people have created some clever algorithms that make it relatively straight forward. Here they are:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8H1DcRwt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrlb5kbg1fl3ucewg5n6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8H1DcRwt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrlb5kbg1fl3ucewg5n6.png" alt="Binary to gray pseudocode" width="738" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--henW3Ey2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyd7scps59fg2sslc7vz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--henW3Ey2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyd7scps59fg2sslc7vz.png" alt="Gray to binary pseudocode" width="800" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's how all we need!&lt;/p&gt;

&lt;p&gt;Now, let's have some fun with Gray Code! As I demonstrated in my last &lt;a href="https://dev.to/max_feige/demystifying-bitwise-operators-a-visual-perspective-38kl"&gt;article&lt;/a&gt;, you can create interesting images using the bit representation of numbers. Let's see how different bitwise operators - AND, OR, XOR - behave when applied to Gray Code:&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Note: These are stretched a bit in the x direction, and I tend to like them that way. If you want to see the square images, resizing the images should give you something indistinguishable. &lt;/small&gt;&lt;/p&gt;

&lt;p&gt;First we can compare normal binary to gray code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FhHDoJRL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rliixm6yg56he5qw6o2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FhHDoJRL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rliixm6yg56he5qw6o2.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
Binary gradient based on x coordinate



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cCdGTL_n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4kaib9w93lph5r7e3a9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cCdGTL_n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4kaib9w93lph5r7e3a9.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
Gray code gradient based on x coordinate



&lt;p&gt;Now lets look at our bitwise operators. We'll consider two cases: First we convert our numbers to gray code, perform the bitwise operator, then convert them back to decimal.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CXHi5IgI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edn6fhf76r5p3sk8n6ti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CXHi5IgI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edn6fhf76r5p3sk8n6ti.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
Normal OR



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Iue2HtEm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcgglk0fyeldu82iazgj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Iue2HtEm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcgglk0fyeldu82iazgj.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
Normal AND



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Vv6PqNSu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkn1rmkdsnrv7hy2lz3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Vv6PqNSu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkn1rmkdsnrv7hy2lz3x.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
Normal XOR



&lt;p&gt;You might notice something peculiar about the image showcasing the Normal XOR operation. The apparent 'blockiness' isn't a result of image artifact compression or a mistake - it's actually an intriguing characteristic that arises from the Gray Code representation!&lt;/p&gt;

&lt;p&gt;What's even more fascinating is that this Normal Gray Code XOR operation yields a result entirely equivalent to the binary XOR operation. This might seem counter-intuitive - I encourage you to take a look at the previous code and see if you can solve this puzzle. Unpacking these mysteries is part of what makings coding so exciting!&lt;/p&gt;

&lt;p&gt;Now let's consider the concept of "casting". By casting, I mean performing our operation in Gray Code but not converting the numbers back to binary. While not typically used, this process can generate some fascinating patterns:&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Inspired by this famous &lt;a href="https://en.wikipedia.org/wiki/Fast_inverse_square_root"&gt;algorithm&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oLLD3kYG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xk23ch2xuycik9ch6168.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oLLD3kYG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xk23ch2xuycik9ch6168.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
Casted OR



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QTuPV0OJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhn6paiih5fx87svdlx9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QTuPV0OJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhn6paiih5fx87svdlx9.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
Casted AND



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--axoiHvfQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3o58ybcecl4uty6ca78d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--axoiHvfQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3o58ybcecl4uty6ca78d.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
Casted XOR



&lt;p&gt;I find these patterns absolutely captivating. The process of casting in Gray Code produces an effect reminiscent of layering transparent images, creating a captivating visual representation of this weird binary sibling.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>graphics</category>
      <category>opengl</category>
    </item>
    <item>
      <title>Demystifying Bitwise Operators: A Visual Perspective</title>
      <dc:creator>Max Feige</dc:creator>
      <pubDate>Sun, 14 May 2023 20:52:12 +0000</pubDate>
      <link>https://dev.to/max_feige/demystifying-bitwise-operators-a-visual-perspective-38kl</link>
      <guid>https://dev.to/max_feige/demystifying-bitwise-operators-a-visual-perspective-38kl</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever found bitwise operators mysterious, despite their simple nature? Their function is clear, but their application and the impact they have on their inputs have eluded me. However, after my recent foray into GLSL &lt;a href="https://thebookofshaders.com/"&gt;shaders&lt;/a&gt; it dawned on me that they would be an excellent tool to visualize what these bitwise operators are actually doing. This led to my experiment of using shaders to generate images of bitwise operators.&lt;/p&gt;

&lt;h2&gt;
  
  
  Image Generation
&lt;/h2&gt;

&lt;p&gt;Each generated image is a square with dimensions 1024×1024 pixels. The generation process involves evaluating each pixel individually. We take into account the current x and y coordinates, and apply our selected operation to them. This operation produces a result between 0 and 1023. To convert this result into a measure of brightness, we divide by the maximum possible value (1023). The process, in pseudocode, would look something akin to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--88xftI32--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr32d9ngu60sjkzbls3o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--88xftI32--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr32d9ngu60sjkzbls3o.png" alt="Generation Pseudocode" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Operators
&lt;/h2&gt;

&lt;p&gt;Let’s dive into the world of bitwise operators: the | (OR) operator, the &amp;amp; (AND) operator, and the ^ (XOR) operator.&lt;/p&gt;




&lt;h3&gt;
  
  
  OR
&lt;/h3&gt;

&lt;p&gt;The OR (|) operator looks at each bit in each number, and if it see’s any 1’s it writes a 1 to the resultant bit. Otherwise its a 0. Let’s illustrate this with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;010110 |
100100 =
110110
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not too complicated right? Let’s see what our operator looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iDl4UTR7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouizcm2fxnao1libmz9f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iDl4UTR7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouizcm2fxnao1libmz9f.png" alt="Greyscale image of bitwise OR" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wow! That’s a lot brighter than what I expected. Now there’s a few patterns here you may notice.&lt;/p&gt;

&lt;p&gt;Our image seems to be sort composed of squares within squares. Each of these squares is the result of the X or Y coordinate having a bit changing from 0 to 1. The line you see in the middle horizontally represents our Y coordinate 10th bit turning from 0 to 1. Likewise the line vertically represents our X coordinate’s 10th bit turning from 0 to 1.&lt;/p&gt;

&lt;p&gt;Let’s delve into a few other intriguing emergent patterns. Our image tends to get brighter as we go further up and further to the right. This is because, in general, the larger a number is the more bits it will have. In fact, the very last number in our range (1023) is composed entirely of 1’s — meaning no matter what number we OR with it the result will be 1023.&lt;/p&gt;

&lt;p&gt;There’s also this sort of dark diagonal line going on. This line forms because of a few special mathematical properties. The first is that (x | y) will always result in a number greater than or equal to the maximum of x and y, i.e.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(x | y ) ≥ max(x,y)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To decrease a number is to take one of its bits and turn it from on to off. The OR operator is unable to turn bits off, so it cannot decrease any numbers and thus we must have a number that is larger (or at least equal to) both of the inputs.&lt;/p&gt;

&lt;p&gt;So that dark diagonal line is actually forming when X and Y are nearly equal each other. In particular, when X and Y are equal we could say that (x|y) = X = Y. In other words, the diagonal line represents the numbers that affect each other the least in terms of the OR operation!&lt;/p&gt;

&lt;p&gt;Next we have an image I found a bit more interesting:&lt;/p&gt;

&lt;h3&gt;
  
  
  AND
&lt;/h3&gt;

&lt;p&gt;The AND (&amp;amp;) operator scrutinizes each bit in every number. If it spots any 0’s, the resultant bit is set to 0. Otherwise, the resultant bit is set to 1. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;010110 &amp;amp;
100100 =
000100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s take a look at how this operation appears&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8_P68AC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wc7rppz8m51iz3z8g832.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8_P68AC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wc7rppz8m51iz3z8g832.png" alt="Greyscale image of bitwise AND" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again we see the pattern of square subdivisions – for the exact same reason too. Whenever the X or Y coordinate has a bit that flips, the bitwise operation changes drastically.&lt;/p&gt;

&lt;p&gt;You’ll notice a plethora of darker regions in this image, in stark contract to the OR image. Yet these patterns appear for a similar reason.&lt;/p&gt;

&lt;p&gt;Just like the OR operation we can come up with a mathematical equation&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(x &amp;amp; y ) ≤ min(x,y)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Which means our AND operation only has one pixel at peak brightness – all the way at the top right corner!&lt;/p&gt;

&lt;p&gt;Here we see the opposite diagonal pattern of the OR operator. Instead of being at a dark value on the diagonal we see a bright value! This is because the same mathematical fact that was true for OR is true for AND:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(x &amp;amp; y ) = x = y&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The difference this time being that because AND has a tendency to decrease numbers, we see a bright line along the diagonal where it doesn’t decrease them.&lt;/p&gt;

&lt;p&gt;In fact, if you were to pick out each point that is exactly on the diagonal, you would find that for both the OR image and the AND image that they have the same brightness!&lt;/p&gt;

&lt;h3&gt;
  
  
  XOR
&lt;/h3&gt;

&lt;p&gt;The XOR (^) operator observes each bit in every number, and checks to see if they differ. If so, it writes a 1 to the resultant bit. Otherwise, it writes a 0. As an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;010110 ^
100100 =
110010
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s see XOR in action&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zZBChBD8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqy5kupxpuevcy6512b1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zZBChBD8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqy5kupxpuevcy6512b1.png" alt="Greyscale image of bitwise XOR" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the most complex of the three basic operations. We still have the same blocky section as before, but a much more interesting pattern&lt;/p&gt;

&lt;p&gt;We have a dark diagonal and a light diagonal that cross through each other right in the origin. This is because, when x=y,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(x ^ y ) = 0&lt;/code&gt;&lt;br&gt;
What about the light diagonal? Well for the XOR operator to produce a bright color, we would require the binary representations of our two numbers to be opposites – i.e. for every 0 in x there should be a 1 in y and vice versa.&lt;/p&gt;

&lt;p&gt;We can also see that, looking at a single square, two of the four quadrants are darker than the others – the upper right and lower left quadrant. This is again because our numbers correlate more in these sections. In both quadrants, as X increases so does Y.&lt;/p&gt;

&lt;p&gt;The XOR operator presents us some delightful complexity with its crisscrossing diagonals. It’s my favorite of the three by far.&lt;/p&gt;

&lt;h2&gt;
  
  
  CODE
&lt;/h2&gt;

&lt;p&gt;You can find code for the shader’s &lt;a href="https://www.shadertoy.com/view/dlKGW1"&gt;here&lt;/a&gt;. Some slight editing will be needed to select the appropriate bitwise operator. Each shader is also available on &lt;a href="https://github.com/Max-M-Feige/upgraded-spoon"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this visual exploration of bitwise operators has shed some light on their intricacies! I originally started on this adventure after reading about &lt;a href="https://graphics.stanford.edu/~seander/bithacks.html"&gt;bithacks&lt;/a&gt;. I’ve crafted a series of other images from OR, XOR, and AND operators based on some different binary representations of numbers, which I’ll dive into on my next post. For now, here’s a sneak peek!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J4I4daD---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vymybg8254mrb67gjdcs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J4I4daD---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vymybg8254mrb67gjdcs.png" alt="Greyscale image of abitwise and in gray numbers" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>visualization</category>
      <category>computerscience</category>
      <category>opengl</category>
    </item>
  </channel>
</rss>
