Broad Network


Binary and Hexadecimal Numbers for ECMAScript

Handling Bytes in ECMAScript Part 1

Writing ECMAScript Module

Foreword: In this part of the series I talk about bytes and the relationship between binary and hexadecimal numbers.

By: Chrysanthus Date Published: 24 Jul 2016

Introduction

This is part 1 of my series, Handling Bytes in ECMAScript. In this part of the series I talk about bytes and the relationship between binary and hexadecimal numbers. You should have read the previous part of the series before reaching here, because this is a continuation.

Base
Now, this is base 4 counting: 0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, - - -. Now this is Base 10 counting, what you are already used to: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, - - -. In computing counting usually begins from zero, it does not matter the base you are using.

To count normally or to count in any base, you just keep adding 1 to the current number to get the next number. As you count, when you reach the number of the base you declare it as ten, that is, 10. You continue with 11, 12, etc. You continue counting! When you reach twice the base, call that 20; when you reach three times the base; call that 30, and so on.

And so this is base 4 counting again, 0+1=1, 1+1=2, 2+1=3, 3+1=10, 10+1=11, 11+1=12, 12+1=13, 13+1=20, 20+1=21, 21+1=22, 22+1=23, 23+1, 30+1=31, 31+1=32, - - -.

One important rule with base counting is that, you should not see the character for the base name in the numbers and you should not also see any character above that. In base 4 counting, you should not see 4, which is the base name and you should not also see 5, 6, 7, etc.

In base 10, which you use in your everyday life, there is no single character for 10 and there is no character above 10 in the counting: 11 for example is made up of 1 followed by 1; 12 is made up of 1 followed by 2, 13 is made up of 1 followed by 3, and so on. 11, 12, 13, etc. are each a number composed of single numbers. Assume you want to count in base 16: you already have single characters for 0, 1, 2, up to 9; you then need new single characters for 10, 11, 12, 13, 14, 15; and for 16, you call it 10. Seventeen would now be 11 in base 16, eighteen would be 12 in base 16, and so on.

In base 2 counting, the character or name 2 and any number above, should not be seen. So base 2 counting is 0, 1, 10, 11, 100, 101, 110, 111, etc. In base two, 1+1 is 10 and not 2, since 2 cannot be seen there. When you add 1 to a base 2 single number, if the single number is 0, you have 1. If it is 1, you have 10 and so write 0 and carry 1. The carry is added to the preceding single number, to have the resulting preceding single number. For base 2, the single number you write is either 0 or 1; if there is any carry, take it to the left again. Continue this way till the end; the final left-most single number may be a carry.

Base 2, Base 10 and Base 16
The following table should counting in base 10, base 2 and base 16, for base ten numbers from 0 to 20.

Base Ten                  Base Two                Base Sixteen
0                   0                   0
1                   1                   1
2                   10                   2
3                   11                   3
4                   100                   4
5                   101                   5
6                   110                   6
7                   111                   7
8                   1000                   8
9                   1001                   9
10                   1010                   A
11                   1011                   B
12                   1100                   C
13                   1101                   D
14                   1110                   E
15                   1111                   F
16                   10000                   10
17                   10001                   11
18                   10010                   12
19                   10011                   13
20                   10100                   14


For base 16, the characters for ten, eleven, twelve, thirteen, fourteen and fifteen are A, B, C, D, E, and F. You can use lowercase instead of uppercase for these base 16 characters, that is: a, b, c, d, e, and f. In the table, each row gives the equivalent number in the different bases.

Some Important Numbers in Computing
32 and 64 are important numbers in computing. 2 raised to the power 32 is written as 2^32 and it is equal to 4,294,967,296. 2 raised to the power 64 is written as 2^64 and it is equal to 18,446,744,073,709,551,616.

Digit and Integer
A whole number is an integer. In the above table, 20 integers have been written for each base. A single character in an integer, no matter the base, is called a digit. For base 2, these digits are called bits. Any integer in base 2 can consist of one or two possible bits, which are 0 or 1.

Computer Names for Bases
In computing, a base ten number is called a Decimal Number; a base two number is called a Binary Number; and a base sixteen number is called a Hexadecimal Number.

Byte and Hexadecimal Numbers
Eight consecutive bits form a byte. So for example, 11001010 is a byte. A byte is a number is base 2, that is, a byte is a binary number. If a binary number is not up to eight bits, in order to make it a byte, you precede it with a number of zeros to make it a byte. As examples, 11 as a byte is 00000011, 100 as a byte is 00000100 and 1101 as a byte is 00001101. A byte has eight bits. The original number may just happen to be eight bits.

Four consecutive bits within a byte can be replaced by the corresponding hexadecimal digit, beginning from the right. So, 11001010 can be written as CA (or ca). When replacing the four bits with a hexadecimal digit, ignore the preceding zeros. So 0010 has the hexadecimal equivalent of 2 and 0101 has the hexadecimal equivalent of 9. You can read these equivalents from the above table. The byte 00100101 is written as 29 in hexadecimal form. If a byte has 4 preceding zeroes, when writing it in hexadecimal form, the 4 preceding zeroes become 0 in hexadecimal. So, 00001011 in hexadecimal form is 0B (or 0b). The preceding zero in 0B is redundant, but it has to be there if you are talking of a byte in hexadecimal. Note. in hexadecimal, B0 is not the same as 0B and for B0, the zero is not redundant; B0 in bits is 10110000. For 00001011 the four preceding zeroes are redundant but the 10110000 the four zeroes on the right are not redundant.

For any numbering system (decimal, binary, hexadecimal, etc) the digits are important (significant) from right to left. At the extreme right you have the least important digit and at the extreme left you have the most important digit.

Word
In computing, a word consists of four consecutive bytes, with the least significant byte on the right and the most significant on the left. An example of a word is, 01100010 01100011 01100100 01100101. A word consists of 32 bits (8 * 4). In computing, the multiplication sign (X) is written as * (the asterisk).

A double word or 2-word, consists of two consecutive words. A double word has 64 bits (8 * 4 * 2).

Whether you are dealing with a single word or a double word, consecutive four bits beginning from the right, can be replaced by a hexadecimal digit in the word.

Least and Most Significant Byte
Consider the following word:

    01100010011000110110010001100101

This word can be written as follows

    01100010 01100011 01100100 01100101

where spaces have been used to separate the bytes. A word consists of 4 bytes. Now, the left most byte is naturally the most significant byte. The right most byte is naturally the least significant byte. As you move from left to right you move from most significant to least significant byte.

It is possible to re-write this word so that the least significant byte is on the left and the most is on the right; and then as you move from left to right you move from least significant byte to most significant byte. To do that, you do not re-arrange the bits in any byte, you re-arrange only the bytes, which are groups of bits, to have,

01100101 01100100 01100011 01100010

from the above example.

Writing Integers in Bytes and Hexadecimal
From time to time you will have to be doing manual (mental) conversion between bytes and hexadecimal. I have talked about this above, but I just want to give you more examples here. The following two words are the same. The first one is written in bytes and the second one is written in hexadecimal digits.

    01100010111000110110010010100101

    62E364A5

You can use lowercase letters for the hexadecimal digits from 10 to 16. So the number can be written as, 62e364a5. To convert bytes to hexadecimal, first of all rewrite the bits in groups of 8 bits. So the above bits would be rewritten as:

    01100010 11100011 01100100 10100101

Next separate each byte into 4 bits. So the above would be rewritten as:

    0110 0010  1110 0011  0110 0100  1010 0101

You then use the above table to replace each 4 bit group with the hexadecimal digit. So the above would be rewritten as,

        6 2 E 3 6 4 A 5

that is

    62E364A5

The reverse process would convert a hexadecimal number into the byte number.

Computer Memory and Byte
The memory is made up of cells in a series, with one cell lying next to another. The content of each cell is a byte. The above number would be in the memory cells as follows:

    01100010 11100011 01100100 10100101

where each byte is in a cell. You can represent these same memory cell contents in hexadecimal as follows:

    62 E3 64 A5

where two hexadecimal digits form a byte.

Character and the Byte
A character exists in a memory cell as a byte. The uppercase letter, B for example in a memory cell is:

    01000010

Written in hexadecimal, it is,

    42

Do not confuse between a number and a digit. In base 2 a digit is either 0 or 1 while a number in base 2 has at least one digit. In base ten, a digit is a single number, while a number consists of at least one base ten digit. In hexadecimal, a digit is a single number or single letter, while a number consists of at least one hexadecimal digit.

Time to take a break. We stop here and continue in the next part of the series.

Chrys

Related Links

Internet Socket and ECMAScript
Handling Bytes in ECMAScript
ECMAScript Bitwise Operators
ECMAScript Module Essentials
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

NEXT

Comments

Become the Writer's Follower
Send the Writer a Message