简体   繁体   English

如何操作Smalltalk中的位?

[英]How to manipulate bits in Smalltalk?

I am currently working on a file compressor based on Huffman decoding. 我目前正在研究基于霍夫曼解码的文件压缩器。 So I have a decoding tree like so: 所以我有一个像这样的解码树:

在此输入图像描述

and I have to encode this tree on an output file by following a certain criteria: 我必须按照一定的标准在输出文件上编码这个树:

"for each leaf, write out a 0 bit, followed by the 8 bits of the corresponding character. Write out the bits in the order bit 7, bit 6, . . ., bit 0, that is high bit first. As a special case, if the byte is 0, write out bit 8, which will be a 0 for a byte value of 0, and 1 for a byte value of 256 (the EOF marker)." “对于每个叶子,写出一个0位,然后写出相应字符的8位。按顺序位7,位6,...,位0写出位,即高位优先。作为特殊位例如,如果字节为0,则写出位8,对于字节值0将为0,对于字节值为256(EOF标记)将为1。 For an internal node, just write a bit 1. 对于内部节点,只需写入1。

So what I plan to do is to create a bit array and add to it the corresponding bits in the specified format. 所以我打算做的是创建一个位数组并添加指定格式的相应位。 The problem is that I don't know how to convert a number to binary in smalltalk. 问题是我不知道如何在smalltalk中将数字转换为二进制。

For example, if I want to encode the first leaf, I would want to do something like 01101011 ie 0 followed by the bit representation of k and then add every bit one by one into the array. 例如,如果我想编码第一个叶子,我想做一些像01101011,即0后跟k的位表示然后将每个位逐个添加到数组中。

I don't know which dialect you are using exactly, but generally, you can access the bits of Integer. 我不知道你正在使用哪种方言,但一般来说,你可以访问Integer的位。 They are modelled as if the representation was in two-complement, with an infinite sequence of bits. 它们被建模为好像表示是双补码的,具有无限的位序列。

 2 is ....0000000000010
 1 is ....0000000000001    
 0 is ....0000000000000 with infinitely many 0 on the left
-1 is ....1111111111111 with infinitely many 1 on the left
-2 is ....1111111111110

This is also true for LargeIntegers, even though they are generally implemented as sign magnitude (the class encodes the sign), two-complement will be emulated. 对于LargeIntegers也是如此,即使它们通常被实现为符号幅度(类对符号进行编码),也将模拟双补码。

Then you can operate with bitAnd: bitOr: bitXor: bitInvert bitShift:, and in some flavours bitAt:put: 然后你可以使用bitAnd:bitOr:bitXor:bitInvert bitShift :,以及某些风格的bitAt:put:

You can access the bits with (2 bitAt: index) where the index starts at 1 for least significant bit, or grows higher. 您可以使用(2 bitAt:index)访问位,其中索引从1开始,用于最低有效位,或者增长更高。 If it's missing, implement it with bitAnd: and bitShift:... 如果它丢失了,用bitAnd:和bitShift实现它:...

For positive, you can ask for the rank of high bit (2 highBit). 对于正数,您可以要求高位(2 highBit)的等级。

All these operations should create a new integer (there's no in place modification possible). 所有这些操作都应该创建一个新的整数(没有可能的修改)。

Conceptually, a ByteArray is a collection of unsigned integers on 8 bits (between 0 and 255), so you can implement a bit Array with them (if it does not already exist in the dialect). 从概念上讲,ByteArray是8位(0到255之间)的无符号整数的集合,因此您可以使用它们实现位数组(如果方言中尚不存在)。 Or you can use an Integer (but won't be able to control size which will be infinite, nor in place mofifications, operations will cost a copy). 或者您可以使用整数(但无法控制无限大小,也不能使用mofifications,操作将花费一份副本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM