Difference between revisions of "W1032 Negative Integers"

From Coder Merlin
(Created page with "== Prerequisites == * W1031 Positive Integers == Background == == References == * [https://en.wikipedia.org/wiki/Method_of_complements Complements] (Wikipedia)")
 
Line 3: Line 3:


== Background ==
== Background ==
What methods might we use to encode negative integers?  As a first approach, perhaps we can use one of the bits of our word to represent the sign.  Let's consider a single-byte word and the number <math>5_{10}</math>.  This may be encoded as:
[[File:Bit-Pattern-Positive-5.png|thumb|center|300px|link=|Positive 5]]
If we were to reserve the most-significant bit, bit seven, to indicate whether our number was positive or negative, we can use the remaining seven bits to store the absolute value of the number.  In such a case, the number <math>-5_{10}</math> would be encoded as:
[[File:Bit-Pattern-Negative-5-Reserved-Bit.png|thumb|center|300px|link=|Negative 5, Reserved Bit]]
Initially this may appear to be a good solution, but let's explore further.


== References ==
== References ==
* [https://en.wikipedia.org/wiki/Method_of_complements Complements] (Wikipedia)
* [https://en.wikipedia.org/wiki/Method_of_complements Complements] (Wikipedia)

Revision as of 14:38, 21 June 2019

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

Prerequisites[edit]

Background[edit]

What methods might we use to encode negative integers? As a first approach, perhaps we can use one of the bits of our word to represent the sign. Let's consider a single-byte word and the number . This may be encoded as:

Positive 5

If we were to reserve the most-significant bit, bit seven, to indicate whether our number was positive or negative, we can use the remaining seven bits to store the absolute value of the number. In such a case, the number would be encoded as:

Negative 5, Reserved Bit

Initially this may appear to be a good solution, but let's explore further.

References[edit]