# Byte (Bit Octet) - Computer storage Unit (8bit)

### Table of Contents

## About

The byte is the smallest unit of computer storage and represents:

- 8 bits.
- or 2 hexadecimal (1 hexa = 4 bit)

Computer storage capacity is measured in bytes.

In today’s large-capacity servers, gigabyte is more often used.

It implements the first concept of buffer as processing data bit by bit would be really slow. Computer are even going now further processing data at the word size (16/32/64)

## Management

### Id and Storage location

### Reading

Accessing an individual byte frequently requires reading or writing a word at once

### Unit

You must use the binary notation for the computer resource (memory, disk)

**100 Mb = 100 * 1024 * 1024 = 100 * 1024**^{2}= 104 857 600 byte

Decimal | Binary | ||||||
---|---|---|---|---|---|---|---|

Value | Si | Value | IEC | JEDEC | |||

1000 | k | kilo- | 1024 | Ki | kibi- | K | kilo- |

1000^{2} | M | mega- | 1024^{2} | Mi | mebi- | M | mega- |

1000^{3} | G | giga- | 1024^{3} | Gi | gibi- | G | giga- |

1000^{4} | T | tera- | 1024^{4} | Ti | tebi- | ||

1000^{5} | P | peta- | 1024^{5} | Pi | pebi- | ||

1000^{6} | E | exa- | 1024^{6} | Ei | exbi- | ||

1000^{7} | Z | zetta- | 1024^{7} | Zi | zebi- | ||

1000^{8} | Y | yotta- | 1024^{8} | Yi | yobi- |

### Conversion

Because a byte is just a binary number (of a maximum of length 8), all byte operations are binary operation.

#### Byte to Decimal Integer

A byte is also an integer between:

- 0 in bit: 00000000
- and 255 in bit: 11111111.

A byte is just a binary number of length 8

```
//parseInt(x, base)
let byteString = '10101101';
let byteInt = parseInt('10101101', 2);
console.log(byteInt)
```

#### Decimal Integer to Byte

A byte is a decimal integer between:

- 0 : the binary 0000000
- and 255: the binary 11111111

In Javascript:

```
let toByteStringFormat = function (byteInt) {
let byteString = byteInt.toString(2); // to string base 2
let byteStringWithLeadingZero = String(byteString).padStart(8, '0')
return byteStringWithLeadingZero;
}
console.log(' 0: '+toByteStringFormat(0));
console.log('100: '+toByteStringFormat(100));
console.log('255: '+toByteStringFormat(255));
```

#### Byte to Hexadecimal

Byte to hexadecimal means generally to go to integer before converting to the hexadecimal base (16)

In Javascript:

```
let byteStringToHexadecimal = function(byteString){
let byteInt = parseInt(byteString,2); // Byte is a number of base 2, we parse the string in base 2
return byteInt.toString(16).toUpperCase(); // Hexadecimal is a number of base 16
}
console.log('00000000 ( 0): '+byteStringToHexadecimal('00000000'));
console.log('01100100 (100): '+byteStringToHexadecimal('01100100'));
console.log('11111111 (255): '+byteStringToHexadecimal('11111111'));
```

#### Conversion table: Byte to Decimal Integer to Hex

All the conversion in a table where the binary column contains the byte.

#### Byte Array to Long

A byte array is an array of byte.

```
let byteArrayToLong = function(/*byte[]*/byteArray) {
let value = 0;
for ( let i = byteArray.length - 1; i >= 0; i--) {
value = (value * 256) + byteArray[i];
}
return value;
};
let longToByteArray = function(/*long*/long) {
let byteArray = [0, 0, 0, 0, 0, 0, 0, 0];
for ( let index = 0; index < byteArray.length; index ++ ) {
var byte = long & 0xff;
byteArray [ index ] = byte;
long = (long - byte) / 256 ;
}
return byteArray;
};
```

### Maximum Decimal by Number of Byte

The maximum decimal by number of Byte (N) is: <MATH> 2^{N*8} </MATH>

Calculate it:

## How Large is a Trillion?

```
1 Kilobyte = 103 = 1000 bytes
1 Megabyte = 106 = 1,000,000 bytes
1 Gigabyte = 109 = 1,000,000,000 bytes
1 Terabyte = 1012 = 1,000,000,000,000 bytes
1 Petabyte = 1015 = 1,000,000,000,000,000 bytes
1 Exabyte = 1018 = 1,000,000,000,000,000,000 bytes
1 Zetabyte = 1021 = 1,000,000,000,000,000,000,000 bytes
1 Yottabyte = 1024 = 1,000,000,000,000,000,000,000,000 bytes
1 million seconds = 11.57 days
1 billion seconds = 31.6 years
1 trillion seconds = 31,688 years
1 million inches = 15.7 miles
1 trillion inches = 15,700,000 miles (30 round trips to the moon)
$1 million = < $ .01 for every person in U.S.
$1 billion = $ 3.64 for every person is U.S.
$1 trillion = $ 3,636 for every person in U.S.
```