# ZXNumber
ZXNumber is a tool to encode and decode floating point/integer literals used by the calculator in the ZX Spectrum ROM.
These numbers are stored in 5 bytes (40 bits). A number can either be a floating point number or an integer.
## Floating point
Floating point numbers are represented with an exponent, a sign bit and a mantissa. The value is ±mantissa × 2<sup>exponent</sup>.
The exponent is 8 bits and has a range from -128 to 126 and is stored in the first byte of the 5 byte number with a bias of 129 (0x81). -128 (0x80) is stored as 0x01, 0 (0x00) is stored as 0x81 and 126 (0x7e) is stored as 0xff. The value 0 is reserved for integers.
The sign bit is stored in top bit of the second byte.
The mantissa is a 1.31 fixed point number in the range 0x8000000 to 0xffffffff (1.0 to 1.999999999534339). It is stored as a big endian number in the last 4 bytes. The top bit is always 1 and is not stored. This bit is instead used for the sign bit.
### Examples
Value | Bytes | Normalized
----- | ----- | ----------
1.0 | `81 00 00 00 00` | 1.0 × 2<sup>0</sup>
-1.0 | `81 80 00 00 00` | -1.0 × 2<sup>0</sup>
pi | `82 49 0f da a2` | +1.570796326734126 × 2<sup>1</sup>
sqrt(6144) | `87 1c c4 70 a0` | +1.224744871258736 × 2<sup>6</sup>
## Integers
Integers in the range -65535 to 65535 can also be stored as integers instead of floating point. The first byte is 0, the second byte is the sign indicator (0x00 for positive numbers, 0xff for negative numbers). The number is stored as a little endian number in the third and fourth byte and the fifth byte is 0. Negative numbers are stored as two's complement numbers.
Zero is always stored as an integer with all 5 bytes set to 0.
### Examples
Value | Bytes
---- | -----
0 | `00 00 00 00 00`
1000 | `00 00 e8 03 00`
-1000 | `00 ff 18 fc 00`
## Encoded literals
When loading literals in the ZX calculator with the 0x34 opcode, the number is given as an encoded literal. The encoding makes it possible to store numbers in less than 5 bytes by using 1 to 4 bytes for the mantissa.
To achieve this, the top two bits of the first byte contains the number of bytes to use for the mantissa minus 1. This means that there are only 6 bits left for the exponent so some exponents have to be stored in an extra byte.
The exponent is stored with a bias of -0x50 (compared to the exponent stored in the 5 byte value). If this value is less than 0x01 or larger than 0x3f, the 6 bits are set to 0 and the exponent is stored in the following byte. This value does is also encoded with the -0x50 bias. This means that floating point values with exponent from -48 to 14 uses one byte for the exponent and all other, including integers, uses two bytes.
If using less than 4 bytes for the mantissa, the remaining bits in the mantissa are set to 0.
### Examples
Value | Precision | Bytes | Stored value
----- | --------- | ----- | ------------
1.0 | 4 | `f1 00 00 00 00` | 1.0
1.0 | 1 | `31 00` | 1.0
pi | 1 | `32 49` | 3.140625
pi | 2 | `32 49 10` | 3.1416015625
pi | 3 | `b2 49 0f db` | 3.141592741
pi | 4 | `f2 49 0f da a2` | 3.1415926535
-1000 | 3 | `80 b0 ff 18 fc` | -1000
1000 | 2 | `40 b0 00 64` | 100
没有合适的资源?快使用搜索试试~ 我知道了~
zxtools
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共4个文件
md:2个
py:1个
gitignore:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
温馨提示
zxtools
资源详情
资源评论
资源推荐
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 4 条
- 1
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![avatar](https://profile-avatar.csdnimg.cn/9979aa5bd3984d0d9859b24d3850753c_weixin_42134097.jpg!1)
沪漂购房记
- 粉丝: 20
- 资源: 4614
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论1