binary: additional notes on encoding and signed numbers from Tetris course

This commit is contained in:
thomasabishop 2023-06-30 08:23:44 +01:00
parent 099fab4446
commit 958d1e0def
2 changed files with 9 additions and 1 deletions

View file

@ -13,7 +13,11 @@ _Encoding_ is the process of establishing a correspondence between sets of binar
> An encoding system maps each symbol to a unique sequence of bits. A computer then interprets that sequence of bits and displays the apppropriate symbol to the user.
The length of the binary number (sometimes called the _word length_ corresponding to bytes, 16-bit, 32-bit etc) that is used to represent a given data set is determined by the number of variations that you require to capture the entire range of the dataset. For example, say we know that there are 18 levels to a computer game. To encode a reference for each level we would need a binary number that is capable of at least 18 total variations. In this instance a 32-bit ($2^{5}$) number would be best because the next smallest (16-bit) would not be sufficient. Our levels would have representations as follows:
The length of the binary number (sometimes called the _word length_) corresponding to 8-bit, 16-bit, 32-bit etc. that is used to represent a given data set is determined by the number of variations that you reqzuire to capture the entire range of the dataset:
> An $n$-bit binary system can encode $2^n$ different things
For example, say we know that there are 18 levels to a computer game. To encode a reference for each level we would need a binary number that is capable of at least 18 total variations. In this instance a 32-bit ($2^{5}$ ) number would be best because the next smallest (16-bit) would not be sufficient. Our levels would have representations as follows:
```
00001 (1)

View file

@ -8,6 +8,10 @@ tags: [binary, binary-encoding]
In order to represent negative integers in binary we use signed numbers. **Signed binary** is basically binary where negative integers can be represented. **Unsigned binary** is standard binary without negative integers.
In order to represent negative integers alonside positive integers a natural approach is to divide the available [encoding space / word length](/Electronics_and_Hardware/Binary/Binary_encoding.md) into two subsets: one for representing non-negative integers and one for representing negative integers.
The primary method for doing this is to use _two's complement_. This method allows for signed numbers in a way that complicates the hardware implementation of the binary arithmetic operations as little as possible.
## Two's complement
Signed numbers can be implemented in binary in a number of ways. The differences come down to how you choose to encode the negative integers. A common method is to use "two's complement".