<?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: Michael Barakat</title>
    <description>The latest articles on DEV Community by Michael Barakat (@mikeybkats).</description>
    <link>https://dev.to/mikeybkats</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%2F1184712%2F1c36a3f3-b055-45b9-b8cf-fe1f15c0922a.jpeg</url>
      <title>DEV Community: Michael Barakat</title>
      <link>https://dev.to/mikeybkats</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mikeybkats"/>
    <language>en</language>
    <item>
      <title>Binary and Hexadecimal Notation: A refresher for Computer Programming</title>
      <dc:creator>Michael Barakat</dc:creator>
      <pubDate>Sat, 26 Apr 2025 23:20:05 +0000</pubDate>
      <link>https://dev.to/mikeybkats/binary-and-hexadecimal-notation-a-refresher-for-computer-programming-32m0</link>
      <guid>https://dev.to/mikeybkats/binary-and-hexadecimal-notation-a-refresher-for-computer-programming-32m0</guid>
      <description>&lt;p&gt;Binary and hexadecimal notation are used everywhere. Let's brushen up on the topic if you're already familiar. For people that are new to it let me know if there are places that need revision for clarity. Pros, let me know where I'm wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Binary notation:
&lt;/h2&gt;

&lt;p&gt;Binary notation represents decimal numbers as bits or ones and zeros. In binary each digit is a bit which can be either 1 or 0.&lt;/p&gt;

&lt;p&gt;This is the number 1 represented as binary: &lt;code&gt;0001&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the number 2 represented as binary: &lt;code&gt;0010&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are four bit places in the example above. That makes them 4-bit binary numbers. The number is read from the rightmost place first or the &lt;em&gt;least significant bit&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0101
   ↑ first digit (least significant bit) in a binary number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2s complement:
&lt;/h3&gt;

&lt;p&gt;All the bits of any given binary number together represent a number in a system called &lt;em&gt;2s complement&lt;/em&gt;, where each bit is a representation of 2^nth power :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The rightmost digit (&lt;em&gt;lsb&lt;/em&gt;) is either 0 or 2^0 power.&lt;/li&gt;
&lt;li&gt;The second from the rightmost digit is either 0 or 2^1 power.&lt;/li&gt;
&lt;li&gt;The third from the rightmost digit is either 2^2 power.&lt;/li&gt;
&lt;li&gt;The fourth from the rightmost digit is either 2^3 power.&lt;/li&gt;
&lt;li&gt;And so on...&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;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;0 (lsb)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2^3&lt;/td&gt;
&lt;td&gt;2^2&lt;/td&gt;
&lt;td&gt;2^1&lt;/td&gt;
&lt;td&gt;2^0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0 or 8&lt;/td&gt;
&lt;td&gt;0 or 4&lt;/td&gt;
&lt;td&gt;0 or 2&lt;/td&gt;
&lt;td&gt;0 or 1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;0010&lt;/code&gt; equals &lt;code&gt;2&lt;/code&gt; as a decimal value.&lt;/p&gt;

&lt;p&gt;2s complement is a system for representing numbers in binary format. Each bit is a power of two starting with 0 at the very rightmost digit.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;8 (2^3)&lt;/th&gt;
&lt;th&gt;4 (2^2)&lt;/th&gt;
&lt;th&gt;2 (2^1)&lt;/th&gt;
&lt;th&gt;1 (2^0)&lt;/th&gt;
&lt;th&gt;binary&lt;/th&gt;
&lt;th&gt;result as integer&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;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&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;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&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;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;3&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;&lt;code&gt;0100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Using 2s complement binary notation, any positive integer can be expressed. I'm not going into negative numbers for now - let's keep it simple. You can figure out any binary number manually with simple addition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1101
   ↑ First bit  = 1

1101
  ↑  Second bit = 0

1101
 ↑   Third bit  = 4

1101
↑    Fourth bit = 8

1 + 4 + 8 = 13
1101      = 13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Number of bits in binary notation
&lt;/h3&gt;

&lt;p&gt;To be explicit, when we talk about bits, this is what is meant:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;4-bit binary notation:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;8-bit binary notation:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0000 0000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;16-bit binary notation:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0000 0000 0000 0000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Binary arithmetic
&lt;/h3&gt;

&lt;p&gt;Basic binary arithmetic like addition and subtraction may not impress you. However, Binary numbers can be used to perform some mathematical wizardry, as you will see.&lt;/p&gt;

&lt;h5&gt;
  
  
  Binary addition
&lt;/h5&gt;

&lt;p&gt;In binary addition two overlapping positive bits will result in a &lt;em&gt;carry over&lt;/em&gt;. See 1 + 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    0001    (1)
  + 0001    (1)
  ------
  = 0010    (2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Binary subtraction
&lt;/h5&gt;

&lt;p&gt;Binary subtraction is straightforward, simply subtract overlapping bits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    0001    (1)
  - 0001    (1)
  ------
  = 0000    (0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Binary Logic: AND and OR
&lt;/h3&gt;

&lt;p&gt;Here's where binary numbers get really fun. Logical operations like &lt;code&gt;AND&lt;/code&gt; and &lt;code&gt;OR&lt;/code&gt; ect on binary numbers are just like a truth table, but flipped on its side like a math(s) operation. Just line them up vertically like a math problem:&lt;/p&gt;

&lt;h5&gt;
  
  
  Logical AND
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;AND&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    1111    (15)
  &amp;amp; 0011    (3)
  ------
  = 0011    (3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Even more spectacular:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;AND&lt;/code&gt; operations can act as what's called &lt;em&gt;bit masking&lt;/em&gt; because only the bits from the "masking" number will remain. For example: binary &lt;code&gt;AND&lt;/code&gt; any number with &lt;code&gt;3&lt;/code&gt; (as we did above &lt;code&gt;0011&lt;/code&gt;), and only the lower two bits will remain.&lt;/p&gt;

&lt;p&gt;This can be useful for checking when a bit is set:&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="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isBitOneOrTwoSet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;bitMask&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="c1"&gt;// 0011 as binary&lt;/span&gt;
&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;// some value&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;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;bitMask&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="n"&gt;isBitOneOrTwoSet&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Challenge: Can you think of a good way to write a function that tells you the bit index of the highest bit set in a binary number?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Common operations with bit masking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0xFF       // Mask for a byte (8 bits)
0xFFFF     // Mask for a word (16 bits)
0x0F       // Mask for lower 4 bits
0xF0       // Mask for upper 4 bits
0x01       // Mask for least significant bit
0x80       // Mask for most significant bit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Logical OR
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;OR&lt;/code&gt; is taking a back seat to &lt;code&gt;AND&lt;/code&gt; in this write up. It works the same way as you would expect.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;OR&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    0001    (1)
  | 0010    (2)
  ------
  = 0011    (3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's most impressive to me about &lt;code&gt;OR&lt;/code&gt; is that at first glance it just looks like addition, but remember it's an &lt;code&gt;OR&lt;/code&gt; operation not addition. Take 1 and 1 for example, its result is 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    0001    (1)
  | 0001    (1)
  ------
  = 0001    (1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a bit "hand wavy", but it may be used in situations where writing to a row of pixels. Suppose an application needs to write to a display. Here is our display. It's a 32w x 9h grid of bits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;each pixel in the display is either on or off&lt;/li&gt;
&lt;li&gt;pixels are written to in blocks of 16-bit chunks (first row of pixels is two 16-bit chunks)&lt;/li&gt;
&lt;li&gt;a pixel is on when the bit related to the pixel is on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the example above if a row looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00000000000000011100000000000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then pixels 17, 18, and 19 are on. To write to other pixels in the row the dev might use bitwise or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    11100000000000000
  | 00000000000001110
  -------------------
  = 11100000000001110
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can imagine how this might be helpful in something like: &lt;code&gt;function drawPixel(x, y);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Challenge: Create your own api for a screen of pixels. What would the drawPixel implementation look like?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hexadecimal Notation
&lt;/h2&gt;

&lt;p&gt;Hexadecimal notation offers a more compact way of viewing and expressing binary numbers. It lets us represent numbers above 9 using a single character.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;hexadecimal number&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;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;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;E&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;F&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A hexadecimal number can be recognized because it always begins with &lt;code&gt;0x&lt;/code&gt;: &lt;code&gt;0x0&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Hexadecimal Anatomy:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Prefix (0x) - indicates hexadecimal formatting of the number&lt;/li&gt;
&lt;li&gt;Digits - values that come after &lt;code&gt;0x&lt;/code&gt; range from 0 to F&lt;/li&gt;
&lt;li&gt;Places - each digit after the &lt;code&gt;0x&lt;/code&gt; represents a &lt;em&gt;nibble&lt;/em&gt; of 4 bits expressed from left to right&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;hexadecimal numbers range from 0 to F:&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;hex&lt;/th&gt;
&lt;th&gt;binary&lt;/th&gt;
&lt;th&gt;integer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0x0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0101&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x6&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0110&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x7&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0111&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x8&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x9&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xA&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xB&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xC&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xD&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1101&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xE&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1110&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xF&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1111&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Hexadecimal Concatenation
&lt;/h3&gt;

&lt;p&gt;One of the benefits of &lt;em&gt;hexadecimal notation&lt;/em&gt; is the way values can be stacked or concatenated to create higher bit representations:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;hex&lt;/th&gt;
&lt;th&gt;# of bits&lt;/th&gt;
&lt;th&gt;binary&lt;/th&gt;
&lt;th&gt;integer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0x1&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x11&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001 0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;16 + 1 = 17&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x111&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001 0001 0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;128 + 16 + 1 = 145&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0x1111&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001 0001 0001 0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4096 + 128 + 16 + 1 = 4241&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;0x11&lt;/code&gt; is the same as &lt;code&gt;0001 0001&lt;/code&gt; which when expressed as integer is 17. So a hexadecimal number is a concatenation expression of each &lt;em&gt;nibble&lt;/em&gt; of 4 bits from left to right - pretty cool right!? This can be expressed as a formula.&lt;/p&gt;

&lt;p&gt;In a two-digit hexadecimal number, the first digit can be thought of as A and the second digit as B. The hex value can be calculated with the formula: &lt;code&gt;A * (2^n number of bits) + B&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0xFF
  ↑ Digit A
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0xFF
   ↑ Digit B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0xFF &amp;lt;-- two digits, making this a concatenation of two 4-bit numbers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above makes the equation: &lt;code&gt;F x 2^4 + F&lt;/code&gt; or &lt;code&gt;15 x 16 + 15 = 255&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;hex&lt;/th&gt;
&lt;th&gt;binary&lt;/th&gt;
&lt;th&gt;formula&lt;/th&gt;
&lt;th&gt;mathematical representation&lt;/th&gt;
&lt;th&gt;integer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0xFF&lt;/td&gt;
&lt;td&gt;concat &lt;code&gt;1111&lt;/code&gt; and &lt;code&gt;1111&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;A x (2^4) + B&lt;/td&gt;
&lt;td&gt;15 x 16 + 15&lt;/td&gt;
&lt;td&gt;255&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xEF&lt;/td&gt;
&lt;td&gt;concat &lt;code&gt;1110&lt;/code&gt; and &lt;code&gt;1111&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;A x (2^4) + B&lt;/td&gt;
&lt;td&gt;14 x 16 + 15&lt;/td&gt;
&lt;td&gt;239&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0xEFEF&lt;/td&gt;
&lt;td&gt;concat &lt;code&gt;1110 1111&lt;/code&gt; and &lt;code&gt;1110 1111&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;A x (2^8) + B&lt;/td&gt;
&lt;td&gt;239 x 256 + 239&lt;/td&gt;
&lt;td&gt;61423&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;A four-digit hexadecimal number can be thought of as concatenating two 8-bit hexadecimal values.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What about 12-bit concatenation or other bit lengths?
&lt;/h3&gt;

&lt;p&gt;To concatenate any number of bits, simplify the problem by adding each hexadecimal digit's value individually. Take the value &lt;code&gt;0xBCE&lt;/code&gt; for instance:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0xBCE&lt;/code&gt; = &lt;code&gt;0xB00 + 0x0C0 + 0x00E&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To represent this as binary, we left shift the hexadecimal value by 4 bits for each hexadecimal place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   1011 0000 0000  (0xB00)  = 11 x 16^2
 + 0000 1100 0000  (0x0C0)  = 12 x 16^1
 + 0000 0000 1110  (0x00E)  = 14 x 16^0
 ----------------
 = 1011 1100 1110  (0xBCE)  = (11x16^2) + (12x16^1) + (14x16^0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above is called &lt;em&gt;bit shifting&lt;/em&gt;. In C, the expression below is equivalent to what we did above:&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="c1"&gt;// shifts 'B' 8 bits to the left&lt;/span&gt;
&lt;span class="c1"&gt;// shifts 'C' 4 bits to the left&lt;/span&gt;
&lt;span class="c1"&gt;// Logical OR concatenates `B`, `C` and E! 🙌&lt;/span&gt;
&lt;span class="kt"&gt;uint16_t&lt;/span&gt; &lt;span class="n"&gt;result&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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&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;C&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;E&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;"Concatenation result of B, C and E: %d&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="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Concatenation result of B, C and E: 3022&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Understanding the above concepts may not be immediately practical, but it will help you understand binary operations much better. You can now read a short snippet of code like the below and understand how it's working:&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;uint8_t&lt;/span&gt; &lt;span class="n"&gt;jumpEnd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jump&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// shift 8 bits to the right and mask the lower 8 bits.&lt;/span&gt;
&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;jumpStart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jump&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// mask the lower 8 bits&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>binary</category>
      <category>hexadecimal</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
