bit, byte - what are they?

by shian ⌂, Wednesday, September 02, 2020, 10:57 (27 days ago) @ shian

Modern computers are using digital signals. :confused:

Digital signal can be either: electrical current exists or electrical current not exists in a register. So we get base-2 numeral system or binary numeral system, which uses only two symbols: typically "0" (zero) and "1" (one).

A bit is a binary number, i.e. 0 or 1. This is the only numbers that a digital equipment knows (PC, CPU, RAM, HDD, SSD, Internet, etc). But a bit can only represent 2 numbers, 0 or 1, so we must combine few bits to represent larger numbers, in modern computers a group of 8-bits, also called byte, is the basic data unit.

A byte (8-bits) can represent 256 numbers, 0 to 255. i.e. 2 to the power 8 is 256. Let's see base-10 (Decimal) and base-2 (Binary) side by side:

Dec...Binary
--------------
000...00000000
001...00000001
002...00000010
003...00000011
004...00000100
005...00000101
006...00000110
007...00000111
008...00001000
009...00001001
010...00001010
...
246...11110110
247...11110111
248...11111000
249...11111001
250...11111010
251...11111011
252...11111100
253...11111101
254...11111110
255...11111111

Everything in computers is done with units of byte (8-bits). Now, if you need larger numbers then you just combine bytes, for example 2 bytes (16-bits) can represent 65536 numbers, i.e. 2 to the power 16 is 65536; and 4 bytes (32-bits) can represent 4294967296 numbers, i.e. 2 to the power 32 is 4294967296.


Now, to convert between Binary system that the PC is using and Decimal system that people are using it's a bit tedious. Therefore the solution is to represent Binary numbers (base-2 numbers), which are extremely long, in base-16 (Hexadecimal, or hex) numeral system, since each 4-bits are corresponding to exactly 1 hexadecimal digit (0 to F). As you can see:

Dec: Bin  = Hex
--------------
00:  0000 = 0
01:  0001 = 1
02:  0010 = 2
03:  0011 = 3
04:  0100 = 4
05:  0101 = 5
06:  0110 = 6
07:  0111 = 7
08:  1000 = 8
09:  1001 = 9
10:  1010 = A
11:  1011 = B
12:  1100 = C
13:  1101 = D
14:  1110 = E
15:  1111 = F

Example:

Dec...Binary  ...Hex
--------------------
000...00000000...00
001...00000001...01
002...00000010...02
003...00000011...03
004...00000100...04
005...00000101...05
006...00000110...06
007...00000111...07
008...00001000...08
009...00001001...09
010...00001010...0A
011...00001011...0B
012...00001100...0C
013...00001101...0D
014...00001110...0E
015...00001111...0F
016...00010000...10
...
240...11110000...F0
241...11110001...F1
242...11110010...F2
243...11110011...F3
244...11110100...F4
245...11110101...F5
246...11110110...F6
247...11110111...F7
248...11111000...F8
249...11111001...F9
250...11111010...FA
251...11111011...FB
252...11111100...FC
253...11111101...FD
254...11111110...FE
255...11111111...FF

OK, so for example to convert the base-2 number 11111010 (250 in decimal) to base-16 all we need to do is group the number, from right to left, into groups of 4-bits, i.e. 1111 1010;
Now, 1010 is A, and 1111 is F, therefore 11111010 is the hex number FA. Simple.


Worth to mention a branch of algebra called Boolean algebra, it is using only the binary numbers 0 and 1. In boolean algebra 0 is FALSE and 1 is TRUE. Although boolean algebra is more useful for hardware designers rather than for software programmers - FALSE (0) and TRUE (1) are always the result of logical and relational operators in programming, and this is also very useful for logical decisions (branching statements), as well as a factor in regular math equations.


OK, to sum up:

In Euphoria 3 you will not need bits and bytes unless you are advanced programmer. So don't worry about it.

Anyway, there are high level functions in Euphoria to deal with bits: int_to_bits() and bits_to_int()

Also there are high level functions to deal with bytes: int_to_bytes() and bytes_to_int()

There are also lots of low level functions to deal with bits in Euphoria 3 (also in Lib2), such as: and_bits() and eqv_bits() or peek4u().

These low level functions can operate on a single atom (number) or on any arbitrarily (sub)sequence of atoms (numbers).

Where were we? Ha! Bits! Yea. Nice. :lookaround:


BTW, you can tell if a number is negative by the MSB (Most Significant Bit) which is the leftmost bit of a binary number: if it is 1 then the number is negative, if it is 0 then the number is positive. This method is called Two's complement. Again: unless you are advanced programmer - you don't care about it.

Tags:
bit, lesson, byte, boolean


Complete thread:

 RSS Feed of thread

powered by my little forum