#1,079 – The Binary Numeral System

Humans normally represent numeric values using the decimal (base 10) numeral system.  We can also represent numbers as binary, or base 2.

The binary numeral system uses only two digits (numeric symbols), rather than 10.  You represent a numerical value using a string of these binary digits (or bits).  The two digits used are 0 and 1.

Moving from right to left, the digits in a base 2 system represent powers of 2 (1, 2, 4, 8, etc. or 2^0, 2^1, 2^2, 2^3, etc).  The value of a number represented as binary is the sum of the value of each digit multiplied by the appropriate power of 2.  For example:

1079-001

As an example, the binary number 101101 is equivalent to the decimal number 45.

1079-002

 

 

 

 

Advertisements

#194 – Storing a Set of Boolean Values as Bits

You might want to store a set of boolean values in a single variable.  For example, you could keep track of a person’s talents, recording which talents each person has.

E.g.:

  • Fred: good at Singing, Dancing
  • Sally:  good at Dancing, Juggling
  • Ernie: good at Juggling, Joke-Telling, Singing

One way to store this information is to represent each talent with a single bit in a larger word and to then use the entire word to represent a person’s talents.  A bit value of 1 indicates that the person has the talent and 0 indicates that they do not have the talent.  Each person can have 0 or more talents.

For example, we could store information about four talents using four bits:

  • Bit 0 (rightmost) – Singing
  • Bit 1 – Dancing
  • Bit 2 – Juggling
  • Bit 3 (leftmost) = Joke-Telling

Here are the bit patterns for the sets of talents listed above (each word is 4 bits):

  • 0 0 1 1 = Dancing + Singing  (Fred)
  • 0 1 1 0 = Juggling + Dancing  (Sally)
  • 1 1 0 1 = Joke-Telling + Juggling + Singing  (Ernie)