Understanding Base numbering System and Bitwise Operations

CLIFF OYOH OMOLLO - Jul 24 - - Dev Community

Throughout human history, our quest to represent numbers has sparked countless innovations. From the early days of counting on fingers and toes to the intricate hieroglyphs etched in clay, each civilization crafted unique systems. The Greeks introduced the Ionic system, while the Romans famously gave us numerals like I, V, X. Yet, as societies advanced, these methods proved cumbersome to work with. The decimal place system, which has roots in Hindu-Arabic and Chinese traditions,has become the standard we use today.

With the advent of computers, new number systems emerged. Binary, octal, and hexadecimal systems emerged as vital tools in processing data, revolutionizing how we compute and communicate. The binary system (base-2) became fundamental to digital technology, while octal (base-8) and hexadecimal (base-16) systems provided more efficient ways to represent and process large amounts of data. These modern systems replace the familiar base-10 of our everyday arithmetic, marking a significant shift in how we understand and utilize numbers in the digital age.

In this article, we delve into the intricacies of these different numbering systems and explore their practical applications in modern computing. Additionally, we'll unravel the mysteries of bitwise operations, showcasing how they empower programmers to manipulate data at the most fundamental level.

Algebra behind the decimal system

Each position in a decimal number represents a power of 10. Each place value to the left of the decimal point increases by a power of 10.

The expression;

m × 103 + n × 102 + o × 101 + p × 100

where m, n, o, p take on any value between 0 and 9, describes any whole number between 0 and 9999. By including

q × 10-1 + r × 10-2 + s × 10-3 + t × 10-4

where q, r, s, t take on any value between 0 and 9, any decimal number between 0 and 9999.9999 can be represented.

Indices bring the notation alive and reveal the true underlying pattern:

. . . m103 + n102 + o101 + p100 + q10-1 + r 10-2 + s10-3 + t10-4 . . . .

Remember that any number raised to the power 0 equals 1. By adding extra terms both left and right, any number can be accommodated.

In this example, 10 is the base, which means that the values of m to t range between 0 and 9, 1 less than the base. Therefore, by substituting B for the base we have

. . . mB3 + nB2 + oB1 + pB0 + qB-1 + rB-2 + sB-3 + tB-4 . . .

where the values of m to t range between 0 and B − 1.

Octal Numbers

The Octal number system has B = 8 and m to t range between 0 and 7.

. . . m83 + n82 + o81 + p80 + q8-1 + r 8-2 + s8-3 + t8-4 . . .

and the first 1 octal numbers are

18, 28, 38, 48, 58, 68, 78, 108, 118, 128, 138, 148, 158, 168, 178.

Octal numbering (base-8), has no digits ‘8’ or ‘9’ in Octal representation, as the highest single-digit number in octal ‘7’. So decimal ‘8’ in Octal will be 10. To understand how decimal ‘8’ converts to octal 10 we can use this steps:

  1. Divide the decimal number by 8.
  2. Record the remainder.
  3. Divide the quotient by 8 again, recording the remainder each time until the quotient is zero.
  4. The octal number is the remainders read in reverse order.

Example 1: converting decimal ‘8’ to Octal will be;

Divide 8 by 8:

Quotient = 1

Remainder = 0

Since the quotient is now 1, we continue:

Divide 1 by 8:

Quotient = 0 (since 1 divided by 8 is less than 1, so the quotient is 0)

Remainder = 1

Reading the remainders in reverse order, we get 10.

The subscript 8, reminds us that although we may continue to use the words “fifteen”, it is an octal number, and not a decimal. But what is 138 in decimal? Well, it stands for

1 × 81 + 3 × 80 = 11

Thus 268.38 in decimal equals;

(2 x 82) + (6 x 81) + (8 x 80) + (3 x 8-1)

(2 x 64) + (6 x 8) + (8 x 1) + (3 x 0.125)

(128 + 48 + 8 + 0.375)

=184.375

Counting in octal seems challenging at first only because we're not familiar with it, unlike the decimal system. If humans had developed with 8 fingers instead of 10, we would naturally be counting in octal.

Hexadecimal Numbers

The hexadecimal number system has B = 16, and m to t can be 0 to 15, which presents a slight problem, as we don’t have 15 different numerical characters. Consequently, we use 0 to 9, and the letters A, B, C, D, E, F to represent 10, 11, 12, 13, 14, 15 respectively

. . . m163 + n162 + o161 + p160 + q16-1 + r16-2 + s16-3 + 16-4 . . .

and the first 15 hexadecimal numbers are:

116, 216, 316, 416, 516, 616, 716, 816, 916, A16, B16, C16, D16, E16, F16.

Thus 1E.816 in decimal, equals

(1 × 16) + (E × 1) + (8 × 16-1)

(16 + 14) + (8/16)

=30.5

Binary Numbers

The binary number system has B=2, and m to t are 0 or 1.

.. m23 + n22 + o21 + p20 + q2-1 + r 2-2 + s2-3 + t2-4 . . .

And the first 5 binary numbers are;

12, 102, 112, 1002, 1012.

Step-by-Step Process of Converting Decimal to Binary;

  1. Divide the number by 2.
  2. Record the remainder.
  3. Update the number to be the quotient of the division by 2.
  4. Repeat steps 1-3 until the number is 0.
  5. The binary representation is the sequence of remainders read from bottom to top (last to first).

Example: Convert 29 to Binary

  1. 29 ÷ 2 = 14, remainder = 1
  2. 14 ÷ 2 = 7, remainder = 0
  3. 7 ÷ 2 = 3, remainder = 1
  4. 3 ÷ 2 = 1, remainder = 1
  5. 1 ÷ 2 = 0, remainder = 1

Reading the remainders from bottom to top, the binary representation of 29 is 11101.

Thus, 11011.112 in decimal equals:

(1 × 24) + (1 × 23) + (0 × 22) + (1 × 21) + (1 × 20) + (1 × 2-1) + (1 × 2-2)

(1 × 16) + (1 × 8) + (0 × 4) + (1 × 2) + (1 × 1) + (1 × 0.5) + (1 × 0.25)

(16 + 8 + 2) + (1 + 0.5 + 0.25)

= 26.75

Logic and Bit Operations

Computers represent information using Bits. A bit is a symbol with two possible values, namely 0 and 1. The meaning of the word bit comes from binary digit. A bit can be used to represent a truth value because there are two truth values, namely, true and false, with 1 bit representing True and 0 bit representing False.

At the lowest level, computers have to represent information using simple switches, which can be either on or off. If one switch were used to represent a number, you could only represent the values 0 (switch “off”) or 1 (switch “on”). If you combine multiple bits, you can represent larger numbers. In everyday life, we have the most experience with decimal notation, which uses the digits 0 through 9. But binary notation uses only the digits 0 and 1 to represent numbers.

Bitwise AND (&)

The bitwise AND operation compares each bit of two numbers. If both bits are 1, the result is 1. Otherwise, it's 0. To illustrate this, we’ll look at the following example:

Example: 5 & 3

  1. Convert to binary:
    • 5 in binary: 0101
    • 3 in binary: 0011
  2. Perform AND operation:

0101
&0011
0001

3.Result:

  • 0001 in binary is 1 in decimal.
  • So, 5 & 3 is 1.

Bitwise OR (|)

The bitwise OR operation compares each bit of two numbers. If at least one of the bits is 1, the result is 1. Otherwise, it's 0.

Example: 5 | 3

  1. Convert to binary:
    • 5 in binary: 0101
    • 3 in binary: 0011
  2. Perform OR operation:

0101
|0011
0111

3.Result:

  • 0111 in binary is 7 in decimal.
  • So, 5 | 3 is 7.

Bitwise XOR (^)

The bitwise XOR operation compares each bit of two numbers. If the bits are different, the result is 1. If they are the same, the result is 0.

Example: 5 ^ 3

  1. Convert to binary:
    • 5 in binary: 0101
    • 3 in binary: 0011
  2. Perform XOR operation:

0101
^0011
0110

3.Result:

  • 0110 in binary is 6 in decimal.
  • So, 5 ^ 3 is 6.

Bitwise NOT (~)

The bitwise NOT operation flips each bit of the number (0 becomes 1 and 1 becomes 0). This operation works on a single number.

Example: ~5

  1. Convert to binary:
    • 5 in binary: 0101
  2. Perform NOT operation:

~ 0101
1010

3.Result:

  • In many systems, this operation is performed on 32-bit integers.
  • So, 0101 would be represented as 00000000000000000000000000000101.
  • The result of ~5 would be 11111111111111111111111111111010.

Left Shift (<<)

The left shift operation shifts all bits of a number to the left by a certain number of positions. The bits that are shifted out on the left are discarded, and zeros are shifted in from the right.

Example: 5 << 1

  1. Convert to binary:

    • 5 in binary: 0101
  2. Perform Left Shift by 1 Position:

    • When you left shift by 1, each bit moves one position to the left.
    • The leftmost bit (most significant bit) is discarded.
    • A zero is added to the rightmost bit (least significant bit).
    Original:  0101  (binary for 5)
    Shifted:   1010  (binary for 10)
    

Visual Representation of Left Shift

  • Illustration of the left shift step-by-step:

Original Bits

Position: 3 2 1 0
Bits: 0 1 0 1 (5 in binary)

Shift All Bits to the Left by 1 Position:

Position: 4 3 2 1
New Bits: 1 0 1 0 (after left shift by 1)

  • The bit in position 3 moves to position 4.
  • The bit in position 2 moves to position 3.
  • The bit in position 1 moves to position 2.
  • The bit in position 0 moves to position 1.
  • A zero is added to the new rightmost position (position 0).
    3.Resulting Binary Number:

    • New binary number after the shift: 1010
    • 1010 in binary is 10 in decimal.

Right Shift (>>)

The right shift operation shifts all bits of a binary number to the right by a specified number of positions. The bits that are shifted out on the right are discarded, and zeros are shifted in from the left.

Example: 5 >> 1

  1. Convert to Binary:

    • 5 in binary: 0101
  2. Perform Right Shift by 1 Position:

    • When you right shift by 1, each bit moves one position to the right.
    • The rightmost bit (least significant bit) is discarded.
    • A zero is added to the leftmost bit (most significant bit).
    Original:  0101  (binary for 5)
    Shifted:   0010  (binary for 2)
    

Visual Representation

  • Illustration of the right shift step-by-step:

Original Bits

Position: 3 2 1 0
Bits: 0 1 0 1 (5 in binary)

Shift All Bits to the Right by 1 Position:

Position: - 3 2 1
New Bits: 0 0 1 0 (after right shift by 1)

  • The bit in position 3 moves to position 2.
  • The bit in position 2 moves to position 1.
  • The bit in position 1 moves to position 0.
  • The bit in position 0 is discarded.
  • A zero is added to the new leftmost position (position 3).

3.Resulting Binary Number:

 New binary number after the shift: 0010
- 0010 in binary is 2 in decimal.
Enter fullscreen mode Exit fullscreen mode

Understanding these operations and conversions is fundamental in computer science and digital electronics, as they form the basis for more complex algorithms and data manipulation techniques.

.
Terabox Video Player