<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rukshani Athapathu on Medium]]></title>
        <description><![CDATA[Stories by Rukshani Athapathu on Medium]]></description>
        <link>https://medium.com/@arukshani?source=rss-a2346f967a6c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Jr_JFY6WgvBlCX9N1Wq_Hg.jpeg</url>
            <title>Stories by Rukshani Athapathu on Medium</title>
            <link>https://medium.com/@arukshani?source=rss-a2346f967a6c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 01:06:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arukshani/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[HTTP/2 Flow Control]]></title>
            <link>https://medium.com/coderscorner/http-2-flow-control-77e54f7fd518?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/77e54f7fd518</guid>
            <category><![CDATA[network-programming]]></category>
            <category><![CDATA[http2]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Sat, 11 May 2019 06:15:58 GMT</pubDate>
            <atom:updated>2019-05-11T16:28:47.952Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ka0l4fMcZglncw9iFCYhSg.jpeg" /><figcaption>Image Courtesy: <a href="https://pixabay.com/photos/water-pipes-reservoir-overflow-2438837/">https://pixabay.com/</a></figcaption></figure><p>The need for flow control arises in any system, whenever an entity produces items and another entity consumes them. That is, there should be a balance between the rate of production and the consumption to ensure that the sender can’t overwhelm the receiver. This is important in networking protocols as well.</p><h4>Need for flow control in HTTP/2</h4><p>In HTTP/2, each request/response message that is exchanged between a client and a server is associated with a stream and a single HTTP/2 connection is capable of handling multiple concurrent streams. Therefore, to ensure that streams in a single connection won’t interfere with each other, the flow control is supported, both at the connection and at the stream level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MPXfaQ62riEhFdm0E3VrGA.png" /></figure><p>HTTP/2 provides flow control via WINDOW_UPDATE frame and remember only DATA frames are subject to flow control. With the WINDOW_UPDATE frame, both the client and the server can independently advertise the number of octets that they are ready to receive and then the peer must respect this imposed value. That is the basic theory behind HTTP/2 flow control.</p><h4>Flow Control Window</h4><p>Flow control window is just nothing more than an integer value indicating the buffering capacity of the receiver. Each sender maintains a separate flow control window for each stream and for the overall connection.</p><h4>WINDOW_UPDATE Frame</h4><p>WINDOW_UPDATE frame indicates the number of octets the sender can transmit in addition to the existing flow control window. The legal range is 1 to 2³¹ -1 octets.</p><h4>Initial Flow Control Window Size</h4><p>According to the <a href="https://tools.ietf.org/html/rfc7540">HTTP/2 specification</a>, the initial value for the flow control window is 65,535 octets for both the new streams and the overall connection.</p><p>However, during the HTTP/2 connection establishment phase, both endpoints can adjust this window size by changing the value of SETTINGS_INITIAL_WINDOW_SIZE property in the SETTINGS frame. But keep in mind that this only applies to streams and not to the connection window. Connection flow control window can only be changed using a WINDOW_UPDATE frame.</p><p>When this SETTINGS_INITIAL_WINDOW_SIZE value changes, the receiver must adjust the flow control windows by the difference between the new value and the old value.</p><h4>Maintaining Flow Control Window Size</h4><p>Since the flow control is directional, both endpoints maintain stream level and connection level windows. The basic mechanism to maintaining the flow control windows, defined by the spec, is as follows.</p><ol><li>When the sender sends a flow controlled frame(DATA frame), it reduces the space in both windows(stream and connection window) by the length of the transmitted frame.</li><li>When the receiver consumes data, it can send WINDOW_UPDATE frames to the sender to free up flow control windows. The receiver should send separate WINDOW_UPDATE frames for the stream and the connection.</li><li>When the sender receives the WINDOW_UPDATE frames it should update the corresponding windows by the specified value in the frame.</li></ol><p>An example of flow control is depicted in the following figure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*yXTGPRQGqz0KHN1oNbZ0zw.png" /></figure><p>However, please note that implementors are free to choose any algorithm they see fit, to decide when to send the WINDOW_UPDATE frames.</p><p>Now let’s see an example of HTTP/2 flow control in action.</p><h4>HTTP/2 Flow Control in Action</h4><p>I’m going to use <a href="https://ballerina.io/">ballerina language</a> to build a simple HTTP service(we can get it up and running with a few lines of code) and it will be configured to use HTTP/2 as its protocol. I’ll use <a href="https://nghttp2.org/documentation/package_README.html#nghttp-client">nghttp</a> as my test HTTP/2 client.</p><p>Following is the simple HTTP echo service that retrieves the incoming payload from the request and sends it back to the caller.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2dc4689fb52c135dd8f7819313c5c2e/href">https://medium.com/media/a2dc4689fb52c135dd8f7819313c5c2e/href</a></iframe><p>We can start this service by issuing the following command.</p><p>ballerina run http2-flow-control-test.bal</p><p>Once it is started, you will see a log similar to the following.</p><p>[ballerina/http] started HTTP/WSS endpoint 0.0.0.0:9090</p><p>We are all set now. So let’s use nghttp to send a request to this service. I’m going to send an 82.6kB size of a JSON file via this request to the backend service. Here’s the command.</p><p>nghttp -v -d payload.json --cert myapp.pem https://localhost:9090/echo</p><p>Now let’s observe the nghttp log and analyze the HTTP/2 flow. Here, I’m only going to show you the relevant parts of the logs for clarity.</p><h4><strong>Figure 1</strong></h4><p>First, the client sends the SETTINGS frame with the initial window size value as 65535 octets. This tells the server, the number of octets, the client is ready to receive and it is followed by a few priority frames (we are not gonna talk about that right now. That’s a whole other topic) after which the client immediately starts to send headers and data frames to the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*_6xx3YBoNfHjPAn4_bdxYg.png" /><figcaption>Figure 1</figcaption></figure><p>According to the log, we can see that the client has sent four data frames ( (16384 * 3 + 16383 = 65535 octets) to the server. After that client is not allowed to send any more data since the window is now full(flow control window is now equal to the number of octets the client has sent).</p><h4>Figure 2</h4><p>Then the client receives a SETTINGS frame from the server and the client immediately acknowledges that. Since that does not contain an initial window value, original 65535 will be used as the default. The client also receives the settings ACK from the server which tells the client that the server has received the settings frame, sent by the client(Figure 2).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*LOtQyyGticFbe1uPWuhdjQ.png" /><figcaption>Figure 2</figcaption></figure><h4>Figure 3</h4><p>So far the client has sent 65535 octets to the server and as I have mentioned before, it can’t send any more data until it receives a WINDOW_UPDATE frame from the server, freeing up its window.</p><p>So next the client receives two window update frames each with a size of 65535. Stream id 0 is for the connection window and the stream id 13, in this case, is for the relevant stream. Then the client sends the rest of the data to the server (16384+699 = 17083) in two DATA frames. Altogether now the client has sent 82618 octets which constitutes the full payload.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*wRqJP-oxcDvGsrFYk8RGHA.png" /><figcaption>Figure 3</figcaption></figure><h4>Figure 4</h4><p>Next, the client starts to receive a response from the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*cZTnznauVzcorJLgEXMFkg.png" /><figcaption>Figure 4</figcaption></figure><h4>Figure 5 &amp; 6</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*_jDxg9md7VGtF2aXF4VX7Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*rwYohoL8ejzFwqLcq7oK6w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*rCiNGYIYfhURskheJC0dvg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*KIr3tEmQfpqyCte1L7Z2_A.png" /><figcaption>Figure 5</figcaption></figure><p>First, it receives four DATA frames(8192 * 4 =32768 octets).</p><p>Then the client sends a window update frame to the server, telling it that the client has consumed 32768 octets and to free up the window space maintained by the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*-zEgGhjoDMAq8WTLL42osQ.png" /><figcaption>Figure 6</figcaption></figure><h4>Figure 7 &amp; 8</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*bUDnY1e_wZdaB2D2UxDTHg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*u82ORSpoaGsF-w93jdPJIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*zfa7U-E6L2TrMUWaWEV9DA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*uJE9oORqFzLvZysDyenWaQ.png" /><figcaption>Figure 7</figcaption></figure><p>Then the client receives another 4 DATA frames, out of which 3 frames with the size 8192 and another DATA frame with the size 8191. (8192*3+8191=32767).</p><p>Then the client again sends back a window update with 32767 octets as its value to the server to make room in the server window so that the server is allowed to send more data to the client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*jliPS9xb9h5UVLFCgGl0yw.png" /><figcaption>Figure 8</figcaption></figure><h4>Figure 9 &amp; 10</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*ndrL6HKGoDsNlvojmt53ew.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*E3-1Jilm0t0FzduBQXoSkQ.png" /><figcaption>Figure 9</figcaption></figure><p>Finally, the client receives another two DATA frames with sizes 16384 and 699 respectively which makes up the whole response payload.</p><p>Then as the final step, we can see that the client has sent a GOAWAY frame to the server, to close the connection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*3YZb5X6GB-NTQnR3jcVHOA.png" /><figcaption>Figure 10</figcaption></figure><p>This is basically how the flow control works and you can try this out with different initial window sizes by using nghttp ’s -w flag.</p><h4>Error Handling</h4><p>Following are some of the error conditions that we have to taken into consideration when implementing flow control.</p><ul><li>WINDOW_UPDATE frame with a value 0 results in a stream error of type protocol error.</li><li>Errors on the connection flow control window are treated as connection errors.</li><li>Flow control window of the sender cannot exceed 2³¹-1 octets. If a sender receives a WINDOW_UPDATE frame that causes the flow control window to exceed this value then it should be treated as an error.</li></ul><h4>Other Subtle Facts</h4><p>If you want to know more about the error handling and other subtle facts related to flow control, I suggest you read <a href="https://tools.ietf.org/html/rfc7540#page-23">flow control</a> and <a href="https://tools.ietf.org/html/rfc7540#page-46">window update</a> sections in <a href="https://tools.ietf.org/html/rfc7540">HTTP/2 specification</a> for a detailed explanation. That being said, this topic, most of the time does not directly concern the users or developers unless you want to develop your own HTTP/2 server or client. But knowledge about this might help you troubleshoot issues when working with HTTP/2.</p><h3>References</h3><ul><li><a href="https://tools.ietf.org/html/rfc7540">RFC 7540 - Hypertext Transfer Protocol Version 2 (HTTP/2)</a></li><li><a href="https://nghttp2.org/">Nghttp2: HTTP/2 C Library - nghttp2.org</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77e54f7fd518" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/http-2-flow-control-77e54f7fd518">HTTP/2 Flow Control</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Functions, Circles, Parabolas, and Trigonometry]]></title>
            <link>https://medium.com/@arukshani/functions-circles-parabolas-and-trigonometry-48543a3772da?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/48543a3772da</guid>
            <category><![CDATA[math]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Sat, 20 Oct 2018 08:36:07 GMT</pubDate>
            <atom:updated>2018-10-20T14:44:07.309Z</atom:updated>
            <content:encoded><![CDATA[<h4>Things you should know before studying calculus</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j5iI07ojAm27P5zKcCY8Hw.jpeg" /><figcaption>Image Courtesy: <a href="https://pixabay.com/en/math-symbols-blackboard-classroom-1500720/">Pixabay</a></figcaption></figure><p>These are just a few videos I have put together sometime back and thought to include in a blog, so I have everything in one place. I have originally published them <a href="https://math-and-computer-science.teachable.com/p/calculus">here</a>.</p><p>These few videos will introduce you to functions, linear functions, circles, parabolas and briefly talks about trigonometry.</p><h4>Functions</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F296149794%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F296149794&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F733492466_1280.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1072" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ae0a5d3e791ac33038e4acf320bd0be/href">https://medium.com/media/2ae0a5d3e791ac33038e4acf320bd0be/href</a></iframe><h4>Linear Functions</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F296149891%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F296149891&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F733492634_1280.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1072" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff1cf10e67b27bce1c1883d8fffc8ffa/href">https://medium.com/media/ff1cf10e67b27bce1c1883d8fffc8ffa/href</a></iframe><h4>Circles and Parabolas</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F296149967%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F296149967&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F733492674_1280.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1072" frameborder="0" scrolling="no"><a href="https://medium.com/media/28978e3db6f845b8db8f49ada2adc8ba/href">https://medium.com/media/28978e3db6f845b8db8f49ada2adc8ba/href</a></iframe><h4>Trigonometry</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F296150003%3Fapp_id%3D122963&amp;dntp=1&amp;url=https%3A%2F%2Fvimeo.com%2F296150003&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F733492710_1280.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1072" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef99229f94d8565966e7c4f81065656d/href">https://medium.com/media/ef99229f94d8565966e7c4f81065656d/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48543a3772da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transport Layer Multiplexing and Demultiplexing]]></title>
            <link>https://medium.com/coderscorner/transport-layer-multiplexing-and-demultiplexing-670e16b6bfc?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/670e16b6bfc</guid>
            <category><![CDATA[tcp]]></category>
            <category><![CDATA[network-programming]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Sun, 26 Aug 2018 04:53:58 GMT</pubDate>
            <atom:updated>2018-10-12T08:26:56.187Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v0YpCix878Wd5zMfJZy0SA.jpeg" /><figcaption>Image Courtesy: <a href="https://pixabay.com/en/geometric-design-computer-1732847/">Pixabay</a></figcaption></figure><p>In this post we are going to talk about how multiplexing and demultiplexing happens at the transport layer. Before we jump into the subject, let’s first jog our memory about what we already know about TCP/IP (Transmission Control Protocol/Internet Protocol), so that we are all in the same page.</p><p>Let’s start with processes.</p><h4>What is a Process?</h4><p>Think of a process as a program running within an end system. So how do processes on two different end systems communicate with each other? Well, by exchanging messages across a network and that’s where TPC/IP protocol suit helps us understand how the communication happens between hosts.</p><p>Following diagram roughly sketches out what happens when we send data across a network from one process to another.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*GKQfHP2CBZalg84Lu9ozYA.png" /></figure><p>As you can see here, TCP/IP protocol suit is made up of different layers and remember that each upper level protocol uses services provided by one or more lower level protocols.</p><h4>Different Names for Packets?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*Zc99c2d8FmK8O7mMLx0Ijg.png" /></figure><p>Packets of information that passes through these layers are referred by different names in each layer. At the application level, data that needs to be exchanged is referred to as a <strong>message</strong>. Then the transport layer takes this message and adds transport layer headers to it. The resulting encapsulated packet is called a <strong>segment</strong>. When the segement is passed on to the network layer, it wraps the segment with its own network layer headers and it goes by the name of <strong>datagram</strong>. Then when the link layer takes over these datagrams, it adds link layer related headers and pass those packets which is then known as <strong>frames </strong>to the physical layer for transmission.</p><p>At the destination, the reverse process happens. Each layer decapsulates the receiving packets and handover the payload to the higher level layer until the message reaches the application level. One thing to keep in mind here is that, between the source and the destination, there can be many routers, link layer switches which can contain different set of layers depending on their functionality. For example, routers typically don’t have transport or application layers implemented within them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*QPLrM5eOGApBlErEMW8Qqw.png" /></figure><h4>Process to Process Communication</h4><p>If you look at the network layer, it is only responsible for host to host communication. That is, it can only deliver messages to the destination computer. Once a message has been delivered to the destination, it still needs to be handed over to the correct process and transport layer has the responsibility to take care of that.</p><p>Suppose you have three application processes running in your machine. Now when your computer receives data from outside that message needs to be directed to the correct application process. This is where the sockets come into play and now it is a good time to introduce that.</p><h4>Sockets</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*zqP1uj6l5ime5JvwgQTScg.png" /></figure><p>Transport layer actually does not directly deliver messages to processes. So, who does that? Well, there’s an intermediate software interface called a socket layer that does the transmission of messages between the underline network and the processes. Now remember that a process can have one or more sockets through which it can pass data to and from network and each of these sockets have a unique identifier.</p><h4>So, What is Multiplexing and Demultiplexing?</h4><p>Transport layer gathers chunks of data it receives from different sockets and encapsulate them with transport headers. Passing these resulting segments to the network layer is called multiplexing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*tUOFoRHeIut2-uU_p7QFSg.png" /><figcaption>Multiplexing</figcaption></figure><p>The reverse process which is delivering data to the correct socket by the transport layer is called demultiplexing. But this still doesn’t explain how the transport layer identifies the correct socket. Port numbers are the ones that do the trick.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*BIt3bTjMbNOwLjN5dM_DDw.png" /><figcaption>Demultiplexing</figcaption></figure><h4>Port Numbers</h4><p>Each socket in a host can be assigned a port number. In TCP/IP protocol suit, port numbers are 16-bit numbers ranging from 0 to 65,535. Well known port numbers start from 0 to 1023 and they are reserved for well-known application protocols. For example, HTTP uses port number 80, FTP 21, etc…</p><p>Now let’s examine how these port numbers are useful in understanding the role of transport-layer multiplexing and demultiplexing. When packets arrive, the transport layer looks for the destination port number in the segment that is bound to that port number, but that alone is not enough for the TCP to demultiplex the incoming segment. It has to examine all the four elements(local host, local port, foreign host, foreign port) in the socket pair to uniquely identify the endpoint to which the receiving segment should be directed to. Then the data in that segment passes through the socket into the attached process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*fk79SzTdqrIVpKUOSI3FBg.png" /><figcaption>TCP Segment</figcaption></figure><h4>Client and Server Processes</h4><p>Finally about the client server paradigm. Earlier we briefly talked about process to process communication. In the context of the communication between these processes, remember that the process that initiates the communication is called a <strong>client</strong> and the process that waits to be contacted is called the <strong>server</strong>. To establish a connection between the client and the server, TCP suit needs both the IP address and the port number at each end and this combination of the IP address and the port is called a <strong>socket address</strong>.</p><p>What we have talked so far is just the theoretical stuff. Now to see all this in action you can read the following <a href="https://medium.com/coderscorner/tale-of-client-server-and-socket-a6ef54a74763">post</a> which greatly explains these concepts with code snippets.</p><p><a href="https://medium.com/coderscorner/tale-of-client-server-and-socket-a6ef54a74763">Blocking I/O and non-blocking I/O</a></p><h3>References</h3><ul><li><a href="https://www.amazon.com/Computer-Networking-Top-Down-Approach-6th/dp/0132856204/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1535248099&amp;sr=1-1">Computer Networking: A Top-Down Approach (6th Edition)</a></li><li><a href="https://www.amazon.com/Computer-Networks-Top-Down-Approach/dp/0073523267">Computer Networks: A Top Down Approach</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=670e16b6bfc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/transport-layer-multiplexing-and-demultiplexing-670e16b6bfc">Transport Layer Multiplexing and Demultiplexing</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Code]]></title>
            <link>https://medium.com/coderscorner/machine-code-dd8fcbe3153?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd8fcbe3153</guid>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[machine-code]]></category>
            <category><![CDATA[fundamentals]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Sun, 27 May 2018 10:50:13 GMT</pubDate>
            <atom:updated>2018-06-07T01:07:22.424Z</atom:updated>
            <content:encoded><![CDATA[<h4>Under the hood of your program</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ru1MpzGDMCnDCB5NJ13YqA.jpeg" /><figcaption>Image Courtesy: <a href="https://www.pexels.com/photo/low-angle-photo-of-spiral-ladder-1040489/">Pexels</a></figcaption></figure><p>We write programs all the time using high level programming languages like Java, C etc... With these programs we can instruct the computer to do something useful. But have you ever wondered how exactly the computer is able to execute these instructions? Idea of this post is not to teach you how to program in a low level language or in worst case binary form but to show you what happens under the hood.</p><p>As you might already know, computers work with <a href="https://medium.com/coderscorner/what-exactly-is-in-a-1-bit-of-digital-memory-d5395f9001a6">bits</a>. A bit is something that just represents two states. An on or off, 1 or 0, true or false, plus or minus etc… We can use any two things to represent these two states but in general we use 1 and 0 to say the bit is on and off respectively. We know that computers can store words, pictures, sounds etc… But in reality all these are nothing but bits and we humans have grouped them and given it some meaning (a code). For examples let’s take a byte which is in fact 8 bits. How many patterns can we make up with 8 bits? With 1 bit there are only two possibilities, 1 or 0. With 2 bits, 4 patterns; likewise with 8 bits we have 256 possible ways of arranging bits. So what did people do? They got together and held some meetings and agreed on some code and made it a standard. Take ASCII for example,the bit pattern ‘01000001’ represents letter ‘A’. We just gave some meaning to a bit pattern. But keep in mind that how these patterns are interpreted is based on the context in which it is being used.</p><p>In a high level sense basically what happens when you run a program is, your program gets loaded into the RAM first and then the processor starts to execute the instructions in that program. An instruction is simply a sequence of one or more bytes and different processors follow different <em>Instruction Set Architectures</em>(ISAs) in their instruction encoding. (In Linux, you can use the following command to find out what ISA is being used by your machine. In my case it is <em>x86_64</em>)</p><pre>uname -m</pre><p>Let’s take a simple program written in C to print a “Hello World!” and see what exactly happens under the hood when we run it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa510785b83f733d385b0123d6208abc/href">https://medium.com/media/aa510785b83f733d385b0123d6208abc/href</a></iframe><p>First we write our program in a particular high level language (I have written it in C) with ASCII characters and saved it in a file. Next we will use a special program called a <em>compiler</em> to translate this text file to <em>assembly language</em> statements. Assembly language represents the symbolic version of machine instructions that the hardware can understand and the binary version is called the <em>machine language</em>.</p><p>When I run the following command, it generates a binary file called ‘hello.o’ and if we open it in a text editor it would appear gibberish.</p><pre>gcc -Og -c hello.c</pre><p>Now to view the content of this generated machine code (hello.o), in Linux we have a program called ‘OBJDUMP’ which we can use to view the generated instructions in assembly format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*y2QI2LaJSVa557xH-sK_oQ.png" /><figcaption>Relocatable object file</figcaption></figure><p>Now to generate the actual executable code we need a linker to run on this generated object file(hello.o).</p><pre>gcc -o output hello.o</pre><p>Now again if we disassemble this generated executable file (output) using <em>objdump</em> you will see the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*22eUh49lNhaliOvdP6_csw.png" /><figcaption>Executable file content</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O1eX9ttLq_QZDDj_nKirAw.png" /></figure><p>If you look at the C program that we have written in ASCII, there we have used a <em>printf </em>function which actually is provided by the standard C library. What the linker has done here is, it merged the hello.o program with the pre-compiled printf.o and made an executable file called ‘output’. Main difference you can see here in the assembly code with that of the relocatable object file is that the linker has filled the proper address for <em>callq</em> instruction.</p><p>Now we are ready to load our executable file (output) into main memory and let the processor execute it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*oEdy6OUxx8X8jlBm46Arhg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/164/1*jIbSGuRH3X0_mg3PNGjSzQ.png" /><figcaption>Generated files</figcaption></figure><p><strong>References</strong></p><ul><li><a href="https://www.elsevier.com/books/computer-organization-and-design-mips-edition/patterson/978-0-12-407726-3">https://www.elsevier.com/books/computer-organization-and-design-mips-edition/patterson/978-0-12-407726-3</a></li><li><a href="http://www.buthowdoitknow.com/">http://www.buthowdoitknow.com/</a></li><li><a href="http://catalogue.pearsoned.co.uk/educator/product/Computer-Systems-A-Programmers-Perspective-Global-Edition/9781292101767.page">http://catalogue.pearsoned.co.uk/educator/product/Computer-Systems-A-Programmers-Perspective-Global-Edition/9781292101767.page</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd8fcbe3153" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/machine-code-dd8fcbe3153">Machine Code</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Floating Point Representation]]></title>
            <link>https://medium.com/coderscorner/floating-point-representation-63114653c9ee?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/63114653c9ee</guid>
            <category><![CDATA[floating-points]]></category>
            <category><![CDATA[bit]]></category>
            <category><![CDATA[fundamentals]]></category>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Tue, 22 May 2018 14:44:26 GMT</pubDate>
            <atom:updated>2018-05-22T14:44:26.251Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*twNC_88OkQWubEZR5XEUog.jpeg" /><figcaption>Image Courtesy: <a href="https://www.pexels.com/photo/brown-wooden-wall-shelf-942303/">Pexels</a></figcaption></figure><p>Numbers with fractions that can be put in the form,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/86/1*tWbFTWMfFmfwP_QPEqvXpg.png" /></figure><p>can be represented as floating point numbers in computers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*e6jmux5Soh8mmJK19wJA8A.jpeg" /></figure><p>These numbers are called floating points because the binary point is not fixed. Up until about 1980s different computer manufacturers used different formats for representing floating point numbers, but with the introduction of IEEE standard 754, nowadays almost all the computers follow the said standards which greatly increased the portability of floating point data.</p><h4>IEEE Floating Point Representation</h4><p>IEEE standard defines three formats for representing floating point numbers,</p><ol><li>Single Precision (32 bits)</li><li>Double Precision (64 bits)</li><li>Extended Precision (80 bits)</li></ol><p>and with this standard, floating point numbers are represented in the form,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/197/1*2Xs1KfgAltYA84w-9vMjzg.png" /></figure><p><strong>s </strong>represents the sign of the number. When s=1, floating point number is negative and when s=0 it is positive. <strong>F</strong> represent the fraction (which is also called mantissa) and <strong>E</strong> is the exponent.</p><p>Structure of the two most commonly used formats are shown below.</p><h4>Single Precision (32-bit)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*oJQwA1lg6mESgHxbKqGfsQ.png" /></figure><h4>Double Precision (64-bit)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*sCd7sn36wAKZUEf6j-Zz1g.png" /></figure><p>Now let’s see how we can convert a given decimal number to a floating point binary representation. Lets take -4.40625 as an example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/241/1*_I2nusrrI6axjbwgig95Ug.png" /></figure><h4><strong>Step 1:</strong></h4><p>First convert the integral part which is 4 to binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/105/1*5qioT02Ha3FgvPqsUwd43g.png" /></figure><h4>Step 2:</h4><p>Then we can multiply the fractional part repeatedly by 2 and pick the bit that appears on the left of the decimal to get the binary representation of the fractional part. For example,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*63dBpXfZYgVIGVpDhOTseg.png" /></figure><h4>Step 3:</h4><p>Now we need to normalize the number by moving the binary point so that it takes the form,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*oempU6IfBRt3ul6aMAvegQ.png" /></figure><p>One very important thing to remember here is, that the leading 1 bit does not need to be stored since it is implied. That is a clever trick used by the standard to get additional space for fractional part. So for the fractional part(23 or 52 bits) we need only to save 0001101 in this case and fill the rest of the its bits on to the right with 0s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*hJJsBoEm7oHN7XXTrl7kWQ.png" /></figure><h4>Step 4:</h4><p>Now the exponent is represented as a integer in biased form. So if the exponent has k-bits then the bias equals to,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/148/1*5d1i3nYI0chvZMAo9Z4YPQ.png" /></figure><p>Add this bias to the exponent and place it in the exponent section. With single precision, k has 8 bits so the exponent value in this example equals to,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*BxBuRQ2iUO4ZiaPAJYAomg.png" /></figure><p>Finally the sign bit is set according to the original sign of the number. That is 1 for negative and 0 for positive.</p><p>So the decimal value -4.40625 in binary form can be represented as,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*TdDR6TVm27xBN-IJVOS1FQ.png" /></figure><h3>Special Cases — Infinity and NaN</h3><p><strong>Infinity</strong> — When the exponent bits are all ones and the fraction bits are all 0 then the resulting value represents infinity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*K-DQbaPmwd8kmRR40Uf4hw.png" /><figcaption>Infinity</figcaption></figure><p><strong>NaN</strong> — When exponent bits are all ones but the fraction value is non zero then the resulting value is said to be NaN which is short for <em>Not a Number</em>. You get this value when you perform invalid operations like dividing zero by zero, subtracting infinity from infinity etc…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*2guWN-v00m9U63aLGUjN7A.png" /><figcaption>NaN</figcaption></figure><h4>References</h4><ul><li><a href="http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html">http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html</a></li><li><a href="https://www.amazon.de/Computer-Systems-Programmers-Perspective-Global/dp/1292101768">https://www.amazon.de/Computer-Systems-Programmers-Perspective-Global/dp/1292101768</a></li><li><a href="https://www.amazon.com/Computer-Organization-Design-MIPS-Fifth/dp/0124077269">https://www.amazon.com/Computer-Organization-Design-MIPS-Fifth/dp/0124077269</a></li><li><a href="https://www.amazon.com/Structured-Computer-Organization-Andrew-Tanenbaum/dp/0132916525">https://www.amazon.com/Structured-Computer-Organization-Andrew-Tanenbaum/dp/0132916525</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63114653c9ee" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/floating-point-representation-63114653c9ee">Floating Point Representation</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integer Overflow]]></title>
            <link>https://medium.com/coderscorner/integer-overflow-342849a5897e?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/342849a5897e</guid>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[integer-overflow]]></category>
            <category><![CDATA[fundamentals]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Sat, 19 May 2018 08:50:21 GMT</pubDate>
            <atom:updated>2018-05-19T09:08:55.662Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever encountered a situation where adding two positive numbers resulted in a negative number or vice versa? You see such ridiculous results when an <strong><em>overflow </em></strong>occurs with arithmetic operations. That is when the result of an arithmetic operation cannot be contained within the <a href="https://medium.com/coderscorner/signed-and-unsigned-numbers-c8cdc54bff87">word</a> size of the data type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zTP2wv1UzMYCAjO_bjuilg.jpeg" /><figcaption>Image Courtesy: <a href="https://pixabay.com/en/yellow-split-beans-pile-dry-3408521/">Pixabay</a></figcaption></figure><p>Let’s get this clarified with a few examples.</p><h4>Unsigned Addition</h4><p>Let’s say that we have two 4-bit numbers, 12 (in binary 1100) and 7 (in binary 0111). Sum of these two numbers, 12+7 = 19 (in binary 10011) needs 5 bits to represent the result. If we truncate the bits with weight greater than 2³ that is if we discard the higher order bit, we are left with 0011 which is 3. This is actually equivalent to the value 19 mod 16 = 3.</p><p>In C and Java, overflows are not considered as errors.</p><h4>Two’s Complement Addition</h4><p>Since both the positive and negative numbers can be represented with <a href="https://medium.com/coderscorner/signed-and-unsigned-numbers-c8cdc54bff87">two’s complement</a>, we have to decide what to do when the resulting value of a two’s complement addition becomes too large or too small to represent with a given number of bits. As we have seen <a href="https://medium.com/coderscorner/signed-and-unsigned-numbers-c8cdc54bff87">here</a>, with <strong><em>w bits,</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/213/1*VDp0nS14QIe5SExC3fJTjw.png" /></figure><p>When a sum of two numbers exceeds the max value, we call that a <strong><em>positive overflow</em></strong> and when the result is less than min value, we call it a <strong><em>negative overflow</em></strong>. Let’s see this in action with 4-bits.</p><p>The maximum positive value that can be represented with 4 bits in two’s complement representation is 7 and the minimum value is -8. Following table shows two examples of overflow scenarios.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*-r2BksMQs_Az2sDvgYFDYQ.png" /><figcaption>Two’s Complement Addition Overflow</figcaption></figure><h4>Unsigned Multiplication</h4><p>Just like with addition, in multiplication also we have to deal with overflow scenarios. For example with 4-bits to represent numbers 4 (in binary 0100) and 5(in binary 0101), multiplication (expected value 20[in binary 10100]) gives 4 (in binary 0100) as the result. This is equivalent to, 4.5 mod 2⁴ = 20 mod 16 = 4.</p><h4>Two’s Complement Multiplication</h4><p>With two’s complement numbers, we can derive the multiplication result in following two steps.</p><p>Let’s say we have two numbers x and y each with w-bits, then</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*W2mqWKkqldc5fd8W2c-Etw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*K0LTiq6DqQl_i4IdRXVjVA.png" /><figcaption>Example of a multiplication overflow</figcaption></figure><h4>References</h4><p><a href="https://www.amazon.com/Computer-Systems-Programmers-Perspective-3rd/dp/013409266X">Computer Systems: A Programmer&#39;s Perspective</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=342849a5897e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/integer-overflow-342849a5897e">Integer Overflow</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Byte Ordering]]></title>
            <link>https://medium.com/coderscorner/byte-ordering-e4737416d906?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4737416d906</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[fundamentals]]></category>
            <category><![CDATA[byte]]></category>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[byteorder]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Tue, 15 May 2018 12:17:02 GMT</pubDate>
            <atom:updated>2018-05-17T02:21:09.951Z</atom:updated>
            <content:encoded><![CDATA[<p>In this brief post, we are going to talk about byte order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1lj21ClFEfL6PDsi1ZJSMg.png" /><figcaption>Image Courtesy: <a href="https://pixabay.com/en/binary-0-1-background-3322478/">Pixabay</a></figcaption></figure><p>A byte usually consists of 8 bits and and a collection of bytes form a <a href="https://medium.com/coderscorner/signed-and-unsigned-numbers-c8cdc54bff87">word</a>. Therefore, the bytes in a word can be numbered either from left to right or right to left. This convention of ordering bytes by a machine is called its <strong><em>byte order</em></strong>.</p><p>Think of a variable of an int type which has 32 bits(4 bytes) stored in a contiguous sequence of memory locations 0, 1, 2, 3. Let’s imagine the value of this variable to be 20555768 (in <a href="https://medium.com/coderscorner/hexadecimal-notation-c696eb32328a">hexadecimal notation</a> <em>0x0139A7F8</em>) and the binary representation of that number is,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*a6G7TeDSIZcvLAX_eCQLCg.png" /></figure><h4>Little Endian</h4><p>Some machines store bytes ordered from least significant byte to the most significant and that is called <strong><em>little endian</em></strong>. Here, the least significant byte has the <a href="https://medium.com/coderscorner/hexadecimal-notation-c696eb32328a">hexadecimal</a> value 0xF8 and the most significant byte 0x01.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*cwUGvWSaJ4TYK7q43uz3HQ.png" /><figcaption>Little Endian</figcaption></figure><h4>Big Endian</h4><p>In big endian format, bytes are ordered from most significant to the least significant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*u_b8x4la_OBr26cnwAKkRg.png" /><figcaption>Big Endian</figcaption></figure><p>As programmers, we rarely worry about byte ordering, but this becomes a problem when a little endian machine tries to send binary data to a big endian machine over the network and vice versa. As a solution , TCP/IP requires you to order the bytes in <strong><em>network byte order </em></strong>(which is big endian) before any binary integers in TCP/IP headers are sent across the network.</p><h4>References</h4><p><a href="https://www.amazon.com/Structured-Computer-Organization-Andrew-Tanenbaum/dp/812034720X/ref=asap_bc?ie=UTF8">https://www.amazon.com/Structured-Computer-Organization-Andrew-Tanenbaum/dp/812034720X/ref=asap_bc?ie=UTF8</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4737416d906" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/byte-ordering-e4737416d906">Byte Ordering</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hexadecimal Notation]]></title>
            <link>https://medium.com/coderscorner/hexadecimal-notation-c696eb32328a?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c696eb32328a</guid>
            <category><![CDATA[hexadecimal]]></category>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[fundamentals]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Sun, 13 May 2018 03:49:27 GMT</pubDate>
            <atom:updated>2018-05-26T00:53:05.281Z</atom:updated>
            <content:encoded><![CDATA[<p>Hexadecimal uses digits 0–9 and characters staring from ‘A’ to ‘F’ which altogether contains 16 different values. A single hexadecimal digit can be represented by 4 binary digits (Refer Table 1).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*8j6O7L1gWQOqHyTUIEcEIA.png" /><figcaption>Table 1 : Decimal-Hexa-Binary</figcaption></figure><p>Java and C uses <strong><em>0x</em></strong> or <strong><em>0X</em></strong> to represent hexadecimal numbers. Characters (‘A’ to ‘F’) in a hexadecimal number can be written either in upper case or lower case. For example, 0xa8b3d is same as 0xA8B3D.</p><h4>Converting hexadecimal number to binary</h4><p>Suppose we want to convert the number 0x39A7F8 to binary. This is how you do it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*dpt7fKLvFdBAdoMZDADNIQ.png" /></figure><h4>Converting binary to hexadecimal</h4><p>If we are given the binary 1001101110011110110101 to convert to hexadecimal, first split the binary digits into 4 bit groups and then if the number of bits is not a multiple of 4, padded the left most group with 0s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*lBAsqcVizaGEKtzOcZOMow.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c696eb32328a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/hexadecimal-notation-c696eb32328a">Hexadecimal Notation</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Signed and Unsigned Numbers]]></title>
            <link>https://medium.com/coderscorner/signed-and-unsigned-numbers-c8cdc54bff87?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8cdc54bff87</guid>
            <category><![CDATA[bit]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[fundamentals]]></category>
            <category><![CDATA[binary]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Fri, 11 May 2018 11:36:03 GMT</pubDate>
            <atom:updated>2018-05-13T00:18:31.638Z</atom:updated>
            <content:encoded><![CDATA[<h4>How computers represent numbers</h4><p>Computers use binary digits ( or in other words bits) to store and process information. A binary digit can be of one of the two values, 0 or 1.</p><p>For example, take the decimal number 59. Binary form of that decimal number 59 is 111011.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*FuHqcgSDztEd54VW1gSu_w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*Fscrpqnbf6sC4-YhF6Inww.png" /></figure><h3><strong>Word</strong></h3><p>Computer<strong> </strong><em>word </em>is a unit of data with a defined bit length that a CPU can process at one go. In recent years, many machines have shifted from 32 bit word sizes to 64 bit word sizes.</p><p>Let’s represent 111011 as a 32 bit word.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*lf8t9fZm_9D6frvaLNXdUA.png" /></figure><p>Here the right most bit is called <strong><em>least significant bit</em></strong> and the left most bit, the <strong><em>most significant bit</em></strong>. With 32 bits, we have 2³² bit patterns which means we can represent numbers from 0 to 2³²-1 (4,294,967,295).</p><p>Computer programs use both the positive and negative numbers, so now we need a way to differentiate negatives from positives.</p><h3><strong>Unsigned encoding</strong></h3><p>If we assume that we have 32 bits, then with unsigned encoding every number between 0 and 2³² -1 has a unique encoding. That is each number in this range will map to a unique bit pattern.</p><p>With <strong><em>w bits</em></strong>, number range is,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/109/1*8M-aox57uO8xERPXeWtwag.png" /></figure><h3>Two’s complement encoding for signed numbers</h3><p>To represent negative values, most computers use two’s complement encoding. With this encoding, most significant bit has a negative weight. So all negative numbers will have 1 in the most significant bit which is also known as the <strong><em>sign bit</em></strong>. Therefore, when the sign bit is 1 the value is negative and when it is 0 the value is positive.</p><p>For example, if we take the binary number 1011 which is a 4 bit word, now with two’s complement, its value can be calculated as follows,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*nQDClvmFd9SE45EEslDZkw.png" /></figure><p>With <strong><em>w bits, </em></strong>number range is,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/1*s3O1sW0q8nJMbOuhxXT7fg.png" /></figure><ul><li>Minimum Value</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*2VVi3HI2qfs2rKRxvHU5KA.png" /></figure><ul><li>Maximum value (Sign bit 0 and all the other bits set to 1)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*wuetA0eWM8fS7tFuytRNNQ.png" /></figure><h3>Zero extension</h3><p>To convert an unsigned number with <em>w-bits </em>to a number with more than w bits, we simply add 0s to the left. Let’s say we have a 16-bit unsigned number and we want to convert this to a 32-bit number, we simply fill the leading 16 bits with 0s. This is called zero extension.</p><ul><li>16-bit Version</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*JxppZzt3w_OghQHOk0Pbzw.png" /></figure><ul><li>32-bit Version</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*6lBa2yoEJ7jLQjTJS7UW9w.png" /></figure><h3>Sign extension</h3><p>To convert a two’s complement number with w bits, to a word which has more than w bits, we can use sign extension.</p><p>For example, let’s say we have number -4 in a 16 bit word and we want to convert this to a 32-bit word.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*6oLO-O0fcfu7UEOTEtgzhA.png" /></figure><p>[Shortcut to negate a two’s complement number is to invert every 0 to 1 and every 1 to 0, and then add one to the result.]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*3K3tmUWwebLAvVEP-HuocA.png" /></figure><p>Now to convert this 16-bit version of -4 to a 32-bit version, we simply copy the sign bit 16 times on to the left.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*vRTH0kF8ujFjtI2GXNbtxA.png" /></figure><p>Well, that’s it!</p><p><strong>References</strong></p><ul><li><a href="https://whatis.techtarget.com/definition/word">https://whatis.techtarget.com/definition/word</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8cdc54bff87" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/signed-and-unsigned-numbers-c8cdc54bff87">Signed and Unsigned Numbers</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Number Systems — Decimal, Binary, Octal and Hexadecimal]]></title>
            <link>https://medium.com/coderscorner/number-systems-decimal-binary-octal-and-hexadecimal-5e567e55ab28?source=rss-a2346f967a6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e567e55ab28</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[fundamentals]]></category>
            <category><![CDATA[binary]]></category>
            <dc:creator><![CDATA[Rukshani Athapathu]]></dc:creator>
            <pubDate>Fri, 01 Dec 2017 01:58:33 GMT</pubDate>
            <atom:updated>2018-08-25T15:26:16.370Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lzEry22HgYi5LNiW9hj9cQ.jpeg" /><figcaption>Image Courtesy : <a href="https://www.pexels.com/photo/123-let-s-go-imaginary-text-704767/">Pexels</a></figcaption></figure><p>Let’s explore few different number systems that are in use today and see how with simple three rules, we can build any number system we want.</p><blockquote><em>In mathematics, a “base” or a “radix” is the number of different digits or combination of digits and letters that a system of counting uses to represent numbers. ~Wiki~</em></blockquote><p>For example,</p><ul><li>Base 10 (<em>Decimal) </em>— Represent any number using 10 digits [0–9]</li><li>Base 2 (<em>Binary</em>) — Represent any number using 2 digits [0–1]</li><li>Base 8 (<em>Octal</em>) — Represent any number using 8 digits [0–7]</li><li>Base 16<em>(Hexadecimal)</em> — Represent any number using 10 digits and 6 characters [0–9, A, B, C, D, E, F]</li></ul><p>In any of the number systems mentioned above, zero is very important as a place-holding value. Take the number 1005. How do we write that number so that we know that there are no tens and hundreds in the number? We can’t write it as 15 because that’s a different number and how do we write a million (1,000,000) or a billion (1,000,000,000) without zeros? Do you realize it’s significance?</p><p>First, we will see how the decimal number system is been built, and then we will use the same rules on the other number systems as well.</p><h3><strong>So how do we build a number system?</strong></h3><p>We all know how to write numbers up to 9, don’t we? What then? Well, it’s simple really. When you have used up all of your symbols, what you do is,</p><ul><li>you add another digit to the left and make the right digit 0.</li><li>Then again go up to until you finish up all your symbols on the right side and when you hit the last symbol increase the digit on the left by 1.</li><li>When you used up all the symbols on both the right and left digit, then make both of them 0 and add another 1 to the left and it goes on and on like that.</li></ul><p>If you use the above 3 rules on a decimal system,</p><ul><li>Write numbers 0–9.</li><li>Once you reach 9, make rightmost digit 0 and add 1 to the left which means 10.</li><li>Then on right digit, we go up until 9 and when we reach 19 we use 0 on the right digit and add 1 to the left, so we get 20.</li><li>Likewise, when we reach 99, we use 0s in both of these digits’ places and add 1 to the left which gives us 100.</li></ul><p>So you see when we have ten different symbols, when we add digits to the left side of a number, each position is going to worth 10 times more than it’s previous one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/0*N8Kmrv9a5acZZROT.png" /></figure><h3><strong>How to read numbers?</strong></h3><p>Let’s take the same decimal number system. There are only two rules actually.</p><ul><li>You have a symbol to represent a quantity [0–9]</li><li>Then the meaning of a digit based on its position — let’s get this clarified a bit.</li></ul><p>Let’s take one digit number ‘8’. This simply means 8, in other words, it is exactly what it says it represents. What about 24? In case of two digits, right digit says what it means, but left digit means ten times what it says. That is, 4 is 4, 2 is 20. Altogether forms 24.</p><p>If we take a three digit number, rightmost digit means what it says, the middle one is ten times what it says, leftmost digit 100 times what it says. Simply if we take number 546, it means 6 + (10 * 4) + (5 * 100) = 546.</p><h3><strong>Binary</strong></h3><p>With binary, we have only two digits to represent a number, 0 and 1 and we are already out of symbols. So what do we do? Let’s apply the same rules that we used on the decimal system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/292/0*kXJtmD7FCmaHCRl4.png" /></figure><p>We make the right digit 0 and add 1 to left, that is, our next number is ‘10’. Then we go up until we used up all our symbols on the right side.So the next number in line is 11.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HxTyBvh0xR1O6Io3.png" /></figure><p>After ‘11’, we put 0s in both these places and add 1 to the left and we get 100.</p><p>Then 101, 110, 111 then 1000 …</p><p>This binary number system is based on two digits and each position is worth two times more than the previous position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/0*KX2FT1td3prMSSIv.png" /></figure><p>Reading a binary number is almost same as reading a decimal. Right digit says what it means, next one means two times the previous one, after that 4 times etc…</p><p>So 101 means 5 in decimal.</p><p>These same rules apply to octal and hexadecimal number systems as well. With octal, we have only 8 digits to represent numbers so once we get to 7 the next number is 10 and in hexadecimal, we have 10 digits and 6 letters to represent numbers. In that case, when we reach 9 next number is represented in the letter ‘A’. Next one ‘B’. Likewise, we go up to letter ‘F’ and after ‘F’ comes ‘10’.</p><p>I’ll just list down few numbers in these 4 different number systems and see whether you can apply the rules that we discussed above to get the next number.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/0*MSDyHMutKMcHWp8t.png" /></figure><p>To understand how computers represent positive and negative numbers, please read <a href="https://medium.com/coderscorner/signed-and-unsigned-numbers-c8cdc54bff87">this</a> and more on hexadecimal can be found <a href="https://medium.com/coderscorner/hexadecimal-notation-c696eb32328a">here</a>.</p><p><strong>References</strong></p><ul><li><a href="http://www.buthowdoitknow.com/">http://www.buthowdoitknow.com/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e567e55ab28" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coderscorner/number-systems-decimal-binary-octal-and-hexadecimal-5e567e55ab28">Number Systems — Decimal, Binary, Octal and Hexadecimal</a> was originally published in <a href="https://medium.com/coderscorner">Coder&#39;s Corner</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>