Difference between revisions of "W1031 Positive Integers"

From Coder Merlin
m (Editorial review and minor corrections)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[File:Binary counter.gif|thumb|Binary counter]]
[[File:Binary counter.gif|thumb|Binary counter]]
== Prerequisites ==
== Prerequisites ==
* [[W1011 Number Systems]]
* [[Number Systems]] (W1011)
* [[W1021 Computer History and Architecture]]
* [[Computer History]] (W1021)
* [[Computer Architecture]] (W1022)


== Background ==
== Background ==
The process of transforming information from one format into another, particularly for storage or transmission, is termed '''encoding'''. The opposite process, transforming the stored form to the original format, is termed '''decoding'''.
The process of transforming information from one format into another, especially for storage or transmission, is called '''encoding'''. The opposite process, transforming the stored form to the original format, is called '''decoding'''.
Because modern-day computers are digital and store data as a series of bits, where each bit can possess only the value of one or zero, ''all data'' that we store must ultimately be represented in this format.
Because modern computers are digital and store data as a series of bits, where each bit can possess only the value of one or zero, ''all data'' that we store must ultimately be represented in this format.


Positive integers may be encoded simply by relying on the binary number system. Given ''n'' bits, we can encode any number from zero through <math>2^n-1</math>.
Positive integers can be encoded simply by relying on the binary number system. Given ''n'' bits, we can encode any number from zero through <math>2^n-1</math>.


<blockquote style="border: solid thin grey;">
<blockquote style="border: solid thin grey;">
{|
{|
|[[File:Egg.jpg|thumb|left|100px|An Egg]]
|[[File:Egg.jpg|thumb|left|100px|An egg]]
|''It is allowed on all hands, that the primitive way of breaking eggs before we eat them, was upon the larger end: but his present Majesty's grandfather, while he was a boy, going to eat an egg, and breaking it according to the ancient practice, happened to cut one of his fingers. Whereupon the Emperor his father published an edict, commanding all his subjects, upon great penalties, to break the smaller end of their eggs.'' - Jonathan Swift, Gulliver's Travels
|"It is allowed on all hands, that the primitive way of breaking eggs before we eat them, was upon the larger end: but his present Majesty's grandfather, while he was a boy, going to eat an egg, and breaking it according to the ancient practice, happened to cut one of his fingers. Whereupon the Emperor his father published an edict, commanding all his subjects, upon great penalties, to break the smaller end of their eggs." —Jonathan Swift, Gulliver's Travels
|}
|}
</blockquote>
</blockquote>


Bits are organized into bytes, which are themselves organized into the addressable memory space of the computer. Each unit of addressable space is called a '''word'''. In order to complete our encoding scheme for positive integers, we have two decisions to make:
Bits are organized into bytes, which are themselves organized into the addressable memory space of the computer. Bytes themselves may further be organized into words. To complete our encoding scheme for positive integers, we have two decisions to make:
* Should the least-significant bit be located in position "0" or position "7" within the byte?
* Should the least-significant bit placed be in position "0" or position "7" within the byte?
* Should the least-significant byte be located in the lowest-address position within a word, or the highest-address position?
* Should the least-significant byte be placed in the lowest-address position within a word, or the highest-address position?


These are decisions about '''Endianness''', which describes the starting point of a sequential ordering of elements.   
These are decisions about '''Endianness''', which describes the starting point of a sequential ordering of elements.   
* '''little-endian''' indicates that the least-significant element is placed in the lowest-enumerated position
* '''Little endian''' indicates that the least-significant element is placed in the lowest-enumerated position
* '''big-endian''' indicates that the least-significant element is placed in the largest-enumerated position
* '''Big endian''' indicates that the least-significant element is placed in the largest-enumerated position


Endianness is an issue of design on a particular platform and both methods are in use. Because a single byte is accessed as a unit, endianness of the bits within a byte tends to be less important than the transmission of a byte via a network. The ordering of the bytes within memory, however, is extremely important, as the proper encoding of data requires that we access the bytes in the proper order.
Endianness is an issue of design on a platform, and both methods are in use. Because a single byte is accessed as a unit, endianness of the bits within a byte tends to be less important than the transmission of a byte via a network. The ordering of the bytes within memory, however, is extremely important because the proper encoding of data requires that we access the bytes in the proper order.


{|  align="center"
{|  align="center"
|[[File:Big-Endian.svg|thumb|Big-Endian]]
|[[File:Big-Endian.svg|thumb|Big endian]]
|[[File:Little-Endian.svg|thumb|Little-Endian]]
|[[File:Little-Endian.svg|thumb|Little endian]]
|}
|}


Line 35: Line 36:
[[File:Byte-Bit-Order.png|center|300px]]
[[File:Byte-Bit-Order.png|center|300px]]


How can we represent the number <math>73_{10}</math>, recalling that this is equivalent to <math>49_{16}</math>? In binary, the integer is represented as:
How can we represent the number <math>73_{10}</math>, recalling that this is equivalent to <math>49_{16}</math>? In binary, the integer is represented as:
[[File:Byte-Bit-Order-0x49.png|center|300px]]
[[File:Byte-Bit-Order-0x49.png|center|300px]]


Note that the bit at position zero is ''least significant'', and represents <math>2^0</math> while the bit at position seven is ''most significant'', and represents the value <math>2^7</math>.
Note that the bit at position zero is ''least significant'', and represents <math>2^0</math> whereas the bit at position seven is ''most significant'', and represents the value <math>2^7</math>.


Let's consider a larger positive integer, such as <math>13,695_{10}</math>, recalling that this is equivalent to <math>35 7F_{16}</math>. The largest positive integer that we're able to store in a single, eight-bit byte is 255 (<math>2^8-1</math>), so we'll need at least two bytes to store this integer.  Two bytes can contain up to <math>2^{16}-1</math> or <math>65,535_{10}</math>. At this point, we have two choices: we can either store the least significant byte in a lower address, or, we can store it in a higher address. This would appear as one of the following:
Let's consider a larger positive integer, such as <math>13,695_{10}</math>, recalling that this is equivalent to <math>35 7F_{16}</math>. The largest positive integer that we're able to store in a single, 8-bit byte is 255 (<math>2^8-1</math>), so we'll need at least 2 bytes to store this integer.  Two bytes can contain up to <math>2^{16}-1</math> or <math>65,535_{10}</math>. At this point, we have two choices: we can either store the least significant byte in a lower address, or we can store it in a higher address. This would appear as one of the following:


{|  align="center"
{|  align="center"
|[[File:Byte-Bit-Order-0x357F-Big-Endian.png|thumb|Big-Endian]]
|[[File:Byte-Bit-Order-0x357F-Big-Endian.png|thumb|Big endian]]
|[[File:Byte-Bit-Order-0x357F-Little-Endian.png|thumb|Little-Endian]]
|[[File:Byte-Bit-Order-0x357F-Little-Endian.png|thumb|Little endian]]
|}
|}


== Key Concepts ==
== Key Concepts ==
{{KeyConcepts|
{{KeyConcepts|
* '''Encoding''' is the process of transforming information from one format into another, particularly for storage or transmission
* '''Encoding''' is the process of transforming information from one format into another, especially for storage or transmission
* '''Decoding''' is the process of transforming the stored or transmitted form to the original format
* '''Decoding''' is the process of transforming the stored or transmitted form to the original format
* ''Positive integers'' may be encoded simply by relying on the binary number system
* ''Positive integers'' can be encoded simply by relying on the binary number system
** Given ''n'' bits, we can encode any number from zero through <math>\color{White}2^n-1</math>
** Given ''n'' bits, we can encode any number from zero through <math>\color{White}2^n-1</math>
* '''Endianness''' describes the starting point of a sequential ordering of elements
* '''Endianness''' describes the starting point of a sequential ordering of elements
** '''little-endian''' indicates that the least-significant element is placed in the ''lowest-enumerated'' position
** '''Little endian''' indicates that the least-significant element is placed in the ''lowest-enumerated'' position
** '''big-endian''' indicates that the least-significant element is placed in the ''largest-enumerated'' position
** '''Big endian''' indicates that the least-significant element is placed in the ''largest-enumerated'' position
}}
}}



Latest revision as of 06:43, 4 December 2022

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Binary counter

Prerequisites[edit]

Background[edit]

The process of transforming information from one format into another, especially for storage or transmission, is called encoding. The opposite process, transforming the stored form to the original format, is called decoding. Because modern computers are digital and store data as a series of bits, where each bit can possess only the value of one or zero, all data that we store must ultimately be represented in this format.

Positive integers can be encoded simply by relying on the binary number system. Given n bits, we can encode any number from zero through .

An egg
"It is allowed on all hands, that the primitive way of breaking eggs before we eat them, was upon the larger end: but his present Majesty's grandfather, while he was a boy, going to eat an egg, and breaking it according to the ancient practice, happened to cut one of his fingers. Whereupon the Emperor his father published an edict, commanding all his subjects, upon great penalties, to break the smaller end of their eggs." —Jonathan Swift, Gulliver's Travels

Bits are organized into bytes, which are themselves organized into the addressable memory space of the computer. Bytes themselves may further be organized into words. To complete our encoding scheme for positive integers, we have two decisions to make:

  • Should the least-significant bit placed be in position "0" or position "7" within the byte?
  • Should the least-significant byte be placed in the lowest-address position within a word, or the highest-address position?

These are decisions about Endianness, which describes the starting point of a sequential ordering of elements.

  • Little endian indicates that the least-significant element is placed in the lowest-enumerated position
  • Big endian indicates that the least-significant element is placed in the largest-enumerated position

Endianness is an issue of design on a platform, and both methods are in use. Because a single byte is accessed as a unit, endianness of the bits within a byte tends to be less important than the transmission of a byte via a network. The ordering of the bytes within memory, however, is extremely important because the proper encoding of data requires that we access the bytes in the proper order.

Big endian
Little endian

Let's consider a single byte, and label the bits from right (least significant bit) to left (most significant bit):

Byte-Bit-Order.png

How can we represent the number , recalling that this is equivalent to ? In binary, the integer is represented as:

Byte-Bit-Order-0x49.png

Note that the bit at position zero is least significant, and represents whereas the bit at position seven is most significant, and represents the value .

Let's consider a larger positive integer, such as , recalling that this is equivalent to . The largest positive integer that we're able to store in a single, 8-bit byte is 255 (), so we'll need at least 2 bytes to store this integer. Two bytes can contain up to or . At this point, we have two choices: we can either store the least significant byte in a lower address, or we can store it in a higher address. This would appear as one of the following:

Big endian
Little endian

Key Concepts[edit]

Key ConceptsKeyConceptsIcon.png
  • Encoding is the process of transforming information from one format into another, especially for storage or transmission
  • Decoding is the process of transforming the stored or transmitted form to the original format
  • Positive integers can be encoded simply by relying on the binary number system
    • Given n bits, we can encode any number from zero through
  • Endianness describes the starting point of a sequential ordering of elements
    • Little endian indicates that the least-significant element is placed in the lowest-enumerated position
    • Big endian indicates that the least-significant element is placed in the largest-enumerated position

References[edit]