© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_2

2. Number Basics

Ron Dai1 
(1)
Seattle, WA, USA
 

What Is a Numeral System?

Many different numeral systems exist because there are specific uses where a certain numeral system is more convenient to use and offers advantages over others. For example:
  • Weight: 1 pound = 16 ounces

  • Length: 1 yard = 3 feet, 1 foot = 12 inches

  • Babylonian numeral: Base 60

../images/485723_1_En_2_Chapter/485723_1_En_2_Figa_HTML.jpg
(from Wikipedia)
  • In Ancient China: Ying/Yang – “binary,” Ba Gua – 8 trigrams

../images/485723_1_En_2_Chapter/485723_1_En_2_Figb_HTML.jpg
(from Wikipedia)
  • Decimal counting
    • Ten symbols: 0 – 9

  • Binary counting
    • Two symbols: 0 and 1

  • Time measurement

One day = 24 hours

One hour = 60 minutes = 3600 seconds

Why Do People Use Decimal Numbers, While Computers Use Binary Numbers?

A simple answer is that human beings have ten fingers and ten toes, but a computer has only two states.

Joking aside, a computer is built with many connections and components (parts) that are used to transfer and store data, as well as to communicate with other components. Most of the storing, transferring, and communicating events happen with digital electronics. Digital electronics use the binary system (ON or OFF). A signal with a series of ON/OFF pulses is equal to a binary number.

../images/485723_1_En_2_Chapter/485723_1_En_2_Figc_HTML.jpg

How to Convert a Number Between Different Numeral Systems

[Math] Conversion between Decimal and Binary:
  1. (1)

    Convert a decimal number to a binary number

    [Example]

    Convert 350 in base 10 number, to a binary number (base 2)

    [Answer]

    In base 10, we can write 350 with this equation:

    350 = 3 *102 + 5 * 101 + 0 * 100

    Notice each coefficient (i.e., 3, 5, and 0) is less than 10, and there is no coefficient for 103 or above.

    Now we want to change it to something like this:

    350 = a * 28 + b * 27 + c * 26 + d * 25 + e * 24 + f * 23 + g * 22 + h * 21 + i * 20

    Notice there is no 29 or above, because we know 350 < 512=29

    350 – 1 * 256 (i.e., 28) = 94 < 128 = 27 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif a = 1, b = 0;

    94 – 1 * 64 (i.e., 26) = 30 < 32 = 25 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif c = 1, d = 0;

    30 – 1 * 16 (i.e., 24) = 14 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif e = 1;

    14 – 1 * 8 (i.e., 23) = 6 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif f = 1;

    6 – 1 * 4 (i.e., 22) = 2 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif g = 1;

    2 – 1 * 2 (i.e., 21) = 0 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif h = 1, i = 0;

    Therefore, 350 = 1 * 28 + 0 * 27 + 1 * 26 + 0 * 25 + 1 * 24 + 1 * 23 + 1 * 22 + 1 * 21 + 0 * 20

    Which means (350)10 = (101011110)2

    The subscript number (10 and 2) indicates its number base.

     
  2. (2)

    Convert a binary number to a decimal number

    [Example]

    Convert binary number 11001001 to a decimal number

    [Answer]

    We rewrite the expression of the binary number as shown below.

    (11001001)2

    = 1 * 27 + 1 * 26 + 0 * 25 + 0 *24 + 1 * 23 + 0 * 22 + 0 * 21 + 1 * 20

    = 128 + 64 + 8 + 1

    = (201)10

    To practice conversion between decimal and binary, I recommend this online game: http://​games.​penjee.​com/​binary-numbers-game/​

     
[Math] Fractions in Decimal and Binary
  1. (3)

    Convert a decimal point number (base 10) to a binary number

    We need to understand how we identify each digit after the decimal point. For example, 4.3256

    Remove integer part “4,” so we have 0.3256.

    0.3256 x 10 = 3.256 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 3 is the 1st digit after the decimal point

    Remove integer part “3,” so we now have 0.256

    0.256 x 10 = 2.56 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 2 is the 2nd digit after the decimal point

    Remove integer part “2,” so we now have 0.56

    0.56 x 10 = 5.6 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 5 is the 3rd digit after the decimal point

    Remove integer part “5,” so we now have 0.6

    0.6 x 10 = 6 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 6 is the 4th digit after the decimal point

    Remove integer part “6,”and we are done.

    The same process applies when we convert a fraction from a decimal to a binary.

    Integer part of “4.3256” is “4,” which is 100 in binary.

    From now on, we only look at the decimal part.

    0.3256 x 2 = 0.6512 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 0 is the 1st digit after the decimal point

    0.6512 x 2 = 1.3024 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 1 is the 2nd digit

    0.3024 x 2 = 0.6048 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 0 is the 3rd digit

    0.6048 x 2 = 1.2096 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 1 is the 4th digit

    0.2096 x 2 = 0.4192 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 0 is the 5th digit

    0.4192 x 2 = 0.8392 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 0 is the 6th digit

    0.8392 x 2 = 1.6784 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 1 is the 7th digit

    ……

    Repeat until we finally get 0, or we see a repeating pattern.

    (100.0101001…)2 is the final answer.

    [Math] Binary arithmetic: Addition, Subtraction, Multiplication, Division, Square root

    Binary addition and subtraction operations follow rules such as these:

    0 + 0 = 0 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 0 - 0 = 0

    0 + 1 = 1 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 1 - 0 = 1

    1 + 0 = 1

    1 + 1 = 0 (carry one) = 10 ../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif 10 - 1 = 1

     

Note

As opposed to the decimal numeral system (a.k.a. base 10 numbers) that we are familiar with, a binary number has 2 as its base and has only 0 or 1 as its representation for every digit. In an addition operation, when any digit reaches 2, it becomes “carry one” to its left digit. However, in a subtraction operation, a digit 0 will need to borrow 2 from its left digit to subtract 1. However, this is the opposite direction of the operation to the “carry one.”

Binary multiplication and division operations follow rules as the following:
  • 0 x 0 = 0

  • 0 x 1 = 0

  • 1 x 0 = 0

  • 1 x 1 = 1

This is an example of division between binary numbers.
  •              11__

  • 11) 1011

  •        −11_

  •           101

  •           −11

  •              10../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gifremainder (r)

Conversion between binary and other numeral systems:
  • Hexadecimal – base 16 number system

Mapping between decimal and hexadecimal:

Hexadecimal:

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

Decimal:

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Since every four digits in a binary forms one hexadecimal digit, to convert a binary number to its hexadecimal, we group every four digits in the binary from the right.

For example, 100 in binary equals 4 in hexadecimal and 1011 in binary is 8 + 2 + 1 = B in hexadecimal. Therefore, 1001011 in binary is 4B in hexadecimal.
  • Octal – base 8 number system

Every three digits in a binary forms one octal digit. We can group every three digits in a binary from the very right and convert it to its octal form.

For example,

To convert 10111011 in binary to its octal result:

Step 1 - group it by every three digits from the right: (10)2(111)2(011)2;

Step 2 - convert every group of up to three digits (0 to 1) to an octal digit (0 to 7): (2)8(7)8(3)8;

Step 3 - the converted octal result is 2738.

Inversely, to convert 273 in octal to its binary format, we convert each octal digit to a three-digit binary number:
  • 2738 = (2)8(7)8(3)8 = (010)2(111)2(011)2 = 0101110112

What Is Bit, Byte, KB, MB, GB, TB, and PB?

Bit means a binary digit, 0 or 1. It is the smallest unit of data.

Byte is a sequence of eight bits.

1 Byte (= 8 Bit), KB, MB, GB, TB, PB

1,024 Bytes = 1 KB

KB:   Kilobyte

1,024 KB      = 1 MB

MB:  Megabyte

1,024 MB    = 1 GB

GB:   Gigabyte

1,024 GB     = 1 TB

TB:    Terabyte

1,024 TB      = 1 PB

PB:    Petabyte

What Is Bitwise?

In computers, an integer number is represented as a sequence of bits in memory. We usually interact with decimal numbers in display through a computer’s graphic user interface. However, its binary forms carry out the actual calculations inside the computer. Bitwise is just a level of operations that involves working with individual bits.

Bitwise operators contain three basic ones:

&../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif AND
  • 0 & 0 = 0 & 1 = 1 & 0 = 0

  • 1 & 1 = 1

  • A logical AND (&) of each bit pair results in a 1, if the first bit is 1 AND the second bit is 1. Otherwise, the result is zero.

  • Examples:

  • 01 & 00 = 00

  • 11111111 & 01100101 = 01100101

|../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gifOR

  • 0 | 0 = 0

  • 0 | 1 = 1 | 0 = 1 | 1 = 1

  • A logical OR (|) of each bit pair results in a 1,

  1. (1)

    if the first bit is 1 OR the second bit is 1.

     
  2. (2)

    or, if both the first and the second bit are 1.

     
  • Otherwise, the result is zero.

  • Examples:

  • 0101 | 0011 = 0111

  • 0010 | 1000 = 1010

^../images/485723_1_En_2_Chapter/485723_1_En_2_Figd_HTML.gif NOT
  • A unary operation performs a logical negation on each bit.

  • In other words, after this operation, a 1 bit is flipped to a 0 bit and a 0 bit is flipped to a 1 bit.

  • Examples:

  • ^ 0011 = 1100

  • ^ 01010110 = 10101001

Problems

  1. 1.

    Why do computers use binary numbers?

     
  2. 2.

    What do Hexadecimal, Octal, and Bitwise mean?

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.119.199