第七章 操作位和位串(三)
操作位串
要创建新的位串,请使用 $bit 函数将所需位设置为 1:
kill bitstring
set $bit(bitstring, 3) = 1
set $bit(bitstring, 6) = 1
set $bit(bitstring, 11) = 1
使用 $bit 将现有位串中的位设置为 1:
set $bit(bitstring, 5) = 1
使用 $bit 将现有位串中的位设置为 0:
set $bit(bitstring, 5) = 0
由于位串中的第一位是位 1,因此尝试设置位 0 会返回错误:
set $bit(bitstring, 0) = 1
SET $BIT(bitstring, 0) = 1
^
<VALUE OUT OF RANGE>
测试位是否已设置
要测试是否在现有位串中设置了位,还可以使用 $bit 函数:
write $bit(bitstring, 6)
1
write $bit(bitstring, 5)
0
如果测试未明确设置的位,则 $bit 返回 0:
write $bit(bitstring, 4)
0
write $bit(bitstring, 55)
0
显示位
要显示位串中的位,请使用 $bitcount 函数获取位串中位的计数,然后遍历位:
for
.png)


