|
3 | 3 | // |
4 | 4 | // See LICENSE for license information |
5 | 5 |
|
| 6 | +/// Represents a method to encode signed integers in a binary format. |
6 | 7 | public enum SignedNumberRepresentation { |
7 | 8 |
|
| 9 | + /// Signed magnitude representation. |
8 | 10 | case signMagnitude |
| 11 | + /// One's complement representation of negative integers. |
9 | 12 | case oneComplementNegatives |
| 13 | + /// Two's complement representation of negative integers. |
10 | 14 | case twoComplementNegatives |
| 15 | + /// Biased representation with a custom `bias`. |
11 | 16 | case biased(bias: Int) |
| 17 | + /// Base (radix) -2 representation. |
12 | 18 | case radixNegativeTwo |
13 | 19 |
|
14 | 20 | // Generally speaking, there is a natural limit of the maximum and minimum values that Swift's Int type can hold. |
15 | 21 | // So when the bitsCount matches the bit width of the Int type we need to be careful. |
16 | 22 |
|
| 23 | + /** |
| 24 | + Returns a minimum signed integer that is possible to represent in a binary format within `bitsCount` bits using the |
| 25 | + current representation. |
| 26 | + |
| 27 | + - Precondition: Parameter `bitsCount` must be greater than zero. |
| 28 | + - Precondition: For the `SignedNumberRepresentation.biased` representation, the `bias` must be non-negative. |
| 29 | + */ |
17 | 30 | public func minRepresentableNumber(bitsCount: Int) -> Int { |
18 | 31 | precondition(bitsCount > 0) |
19 | 32 |
|
@@ -53,6 +66,13 @@ public enum SignedNumberRepresentation { |
53 | 66 | } |
54 | 67 | } |
55 | 68 |
|
| 69 | + /** |
| 70 | + Returns a maximum signed integer that is possible to represent in a binary format within `bitsCount` bits using the |
| 71 | + current representation. |
| 72 | + |
| 73 | + - Precondition: Parameter `bitsCount` must be greater than zero. |
| 74 | + - Precondition: For the `SignedNumberRepresentation.biased` representation, the `bias` must be non-negative. |
| 75 | + */ |
56 | 76 | public func maxRepresentableNumber(bitsCount: Int) -> Int { |
57 | 77 | precondition(bitsCount > 0) |
58 | 78 |
|
|
0 commit comments