Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HELP! I don't know binary, hexadecimal, octal, and bit-wise

I haven't learned this in programming class before, but now I need to know it. What are some good resources for learning these numbers and how to convert them? I pretty much am going to memorise these like the times table.

like image 797
Strawberry Avatar asked Nov 28 '22 10:11

Strawberry


1 Answers

In our everyday decimal system, the base number, or radix is 10. A number system's radix tells us how many different digits are in use. In decimal system we use digits 0 through 9.

The significance of a digit is radix ^ i, where i is digit's position counting from right, starting at zero.

Decimal number 6789 broken down:

 6  7  8  9              radix ^ i
 │  │  │  │             ──────────────
 │  │  │  └── ones       10 ^ 0 = 1
 │  │  └───── tens       10 ^ 1 = 10
 │  └──────── hundreds   10 ^ 2 = 100
 └─────────── thousands  10 ^ 3 = 1000

  ones      tens       hundreds    thousands
  ───────────────────────────────────────────────
  (9 * 1) + (8 * 10) + (7 * 100) + (6 * 1000)
= 9       + 80       + 700       + 6000
= 6789

This scheme will help us understand any number system in terms of decimal numbers.


Hexadecimal system's radix is 16, so we need to employ additional digits A...F to denote 10...15. Let's break down hexadecimal number CDEFh in a similar fashion:

 C  D  E  F              radix ^ i
 │  │  │  │             ──────────────
 │  │  │  └── ones       16 ^ 0 = 1
 │  │  └───── sixteens   16 ^ 1 = 16
 │  └──────── 256:s      16 ^ 2 = 256
 └─────────── 4096:s     16 ^ 3 = 4096

  ones       sixteens    256:s        4096:s
  ───────────────────────────────────────────────
  (Fh * 1) + (Eh * 16) + (Dh * 256) + (Ch * 4096)
= (15 * 1) + (14 * 16) + (13 * 256) + (12 * 4096)
= 15       + 224       + 3328       + 49152
= 52719

We have just converted the number CDEFh to decimal (i.e. switched base 16 to base 10).


In binary system, the radix is 2, so only digits 0 and 1 are used. Here is the conversion of binary number 1010b to decimal:

 1  0  1  0              radix ^ i
 │  │  │  │             ──────────────
 │  │  │  └── ones       2 ^ 0 = 1
 │  │  └───── twos       2 ^ 1 = 2
 │  └──────── fours      2 ^ 2 = 4
 └─────────── eights     2 ^ 3 = 8

  ones      twos      fours     eights
  ───────────────────────────────────────────────
  (0 * 1) + (1 * 2) + (0 * 4) + (1 * 8)
= 0       + 2       + 0       + 8
= 10

Octal system - same thing, radix is 8, digits 0...7 are in use. Converting octal 04567 to decimal:

 4  5  6  7              radix ^ i
 │  │  │  │             ──────────────
 │  │  │  └── ones       8 ^ 0 = 1
 │  │  └───── eights     8 ^ 1 = 8
 │  └──────── 64:s       8 ^ 2 = 64
 └─────────── 512:s      8 ^ 3 = 512

  ones      eights    64:s       512:s
  ───────────────────────────────────────────────
  (7 * 1) + (6 * 8) + (5 * 64) + (4 * 512)
= 7       + 48      + 320      + 2048
= 2423

So, to do a conversion between number systems is to simply change the radix.

To learn about bitwise operators, see http://www.eskimo.com/~scs/cclass/int/sx4ab.html.

1: http://en.wikipedia.org/wiki/RadixIn our everyday decimal system, the base number, or radix is 10. A number system's radix tells us how many different digits are in use. In decimal system we use digits 0 through 9.

The significance of a digit is radix ^ i, where i is digit's position counting from right, starting at zero.

Decimal number 6789 broken down:

 6  7  8  9              radix ^ i
 │  │  │  │             ──────────────
 │  │  │  └── ones       10 ^ 0 = 1
 │  │  └───── tens       10 ^ 1 = 10
 │  └──────── hundreds   10 ^ 2 = 100
 └─────────── thousands  10 ^ 3 = 1000

  ones      tens       hundreds    thousands
  ───────────────────────────────────────────────
  (9 * 1) + (8 * 10) + (7 * 100) + (6 * 1000)
= 9       + 80       + 700       + 6000
= 6789

This scheme will help us understand any number system in terms of decimal numbers.


Hexadecimal system's radix is 16, so we need to employ additional digits A...F to denote 10...15. Let's break down hexadecimal number CDEFh in a similar fashion:

 C  D  E  F              radix ^ i
 │  │  │  │             ──────────────
 │  │  │  └── ones       16 ^ 0 = 1
 │  │  └───── sixteens   16 ^ 1 = 16
 │  └──────── 256:s      16 ^ 2 = 256
 └─────────── 4096:s     16 ^ 3 = 4096

  ones       sixteens    256:s        4096:s
  ───────────────────────────────────────────────
  (Fh * 1) + (Eh * 16) + (Dh * 256) + (Ch * 4096)
= (15 * 1) + (14 * 16) + (13 * 256) + (12 * 4096)
= 15       + 224       + 3328       + 49152
= 52719

We have just converted the number CDEFh to decimal (i.e. switched base 16 to base 10).


In binary system, the radix is 2, so only digits 0 and 1 are used. Here is the conversion of binary number 1010b to decimal:

 1  0  1  0              radix ^ i
 │  │  │  │             ──────────────
 │  │  │  └── ones       2 ^ 0 = 1
 │  │  └───── twos       2 ^ 1 = 2
 │  └──────── fours      2 ^ 2 = 4
 └─────────── eights     2 ^ 3 = 8

  ones      twos      fours     eights
  ───────────────────────────────────────────────
  (0 * 1) + (1 * 2) + (0 * 4) + (1 * 8)
= 0       + 2       + 0       + 8
= 10

Octal system - same thing, radix is 8, digits 0...7 are in use. Converting octal 04567 to decimal:

 4  5  6  7              radix ^ i
 │  │  │  │             ──────────────
 │  │  │  └── ones       8 ^ 0 = 1
 │  │  └───── eights     8 ^ 1 = 8
 │  └──────── 64:s       8 ^ 2 = 64
 └─────────── 512:s      8 ^ 3 = 512

  ones      eights    64:s       512:s
  ───────────────────────────────────────────────
  (7 * 1) + (6 * 8) + (5 * 64) + (4 * 512)
= 7       + 48      + 320      + 2048
= 2423

So, to do a conversion between number systems is to simply change the radix.

To learn about bitwise operators, see http://www.eskimo.com/~scs/cclass/int/sx4ab.html.

like image 95
makes Avatar answered Nov 30 '22 23:11

makes