🖥️ 位运算(Bitwise)#

#bs.bitwise:help

一个位运算操作的集合,为操作二进制数据提供了多种工具。

../../_images/bitwise-light.png ../../_images/bitwise-dark.png

“二进制是一个十分简易的计数系统,但当正确使用时会变得非常强大。”

—艾伦·图灵(Alan Turing)


🔧 函数#

你可以在下方找到此模块中的所有可用函数。


位数统计#

#bs.bitwise:bit_count

将一个数取绝对值后,获取它的二进制形式中的1的个数。

输入:

分数 $bitwise.bit_count.n bs.in:要统计的数字。

输出:

返回值 | 分数 $bitwise.bit_count bs.out:输入数字的二进制形式中的1的个数。

示例:计算并显示12的二进制形式中的1的个数:

# Once
scoreboard players set $bitwise.bit_count.n bs.in 12
function #bs.bitwise:bit_count

# See the result
tellraw @a [{"text":"Number of bits for 12 = ","color":"#e0c7ff"},{"score":{"name":"$bitwise.bit_count","objective":"bs.out"}}]

制作人员:Aksiome


位长度#

#bs.bitwise:bit_length

将一个数取绝对值后,获取它的二进制形式所占的位长度,不包括高位的0。

输入:

分数 $bitwise.bit_length.n bs.in:要获取位长度的数字。

输出:

返回值 | 分数 $bitwise.bit_length bs.out:输入数字所占的位长度。

示例:计算并显示表示12所占的位长度:

# Once
scoreboard players set $bitwise.bit_length.n bs.in 12
function #bs.bitwise:bit_length

# See the result
tellraw @a [{"text":"Number of bits for 12 = ","color":"#e0c7ff"},{"score":{"name":"$bitwise.bit_length","objective":"bs.out"}}]

制作人员:Aksiome、theogiraudet


二的补码#

#bs.bitwise:two_complement

计算一个数字的二的补码。

输入:

分数 $bitwise.two_complement.n bs.in:要计算二的补码的数字。

输出:

返回值 | 分数 $bitwise.two_complement bs.out:输入数字的二的补码。

示例:计算并显示-12的二的补码:

# Once
scoreboard players set $bitwise.two_complement.n bs.in -12
function #bs.bitwise:two_complement

# See the result
tellraw @a [{"text":"Two's complement of -12 = ","color":"#e0c7ff"},{"score":{"name":"$bitwise.two_complement","objective":"bs.out"}}]

制作人员:Aksiome、theogiraudet


逻辑运算#

#bs.bitwise:and

计算两个数字的按位与运算。

输入:

分数 $bitwise.and.a bs.in:第一个数 \(=a\)

分数 $bitwise.and.b bs.in:第二个数 \(=b\)

输出:

返回值 | 分数 $bitwise.and bs.out\(=a \land b\) 的运算结果。

示例:计算并显示 \(-9 \land 57\)

# Once
scoreboard players set $bitwise.and.a bs.in -9
scoreboard players set $bitwise.and.b bs.in 57
function #bs.bitwise:and

# See the result
tellraw @a [{"text":"-9 & 57 = ","color":"#e0c7ff"},{"score":{"name":"$bitwise.and","objective":"bs.out"}}]
#bs.bitwise:or

计算两个数字的按位或运算。

输入:

分数 $bitwise.or.a bs.in:第一个数 \(=a\)

分数 $bitwise.or.b bs.in:第二个数 \(=b\)

输出:

返回值 | 分数 $bitwise.or bs.out\(=a \lor b\) 的运算结果。

示例:计算并显示 \(-9 \lor 57\)

# Once
scoreboard players set $bitwise.or.a bs.in -9
scoreboard players set $bitwise.or.b bs.in 57
function #bs.bitwise:or

# See the result
tellraw @a [{"text":"-9 | 57 = ","color":"#e0c7ff"},{"score":{"name":"$bitwise.or","objective":"bs.out"}}]
#bs.bitwise:xor

计算两个数字的按位异或运算。

输入:

分数 $bitwise.xor.a bs.in:第一个数 \(=a\)

分数 $bitwise.xor.b bs.in:第二个数 \(=b\)

输出:

返回值 | 分数 $bitwise.xor bs.out\(=a \oplus b\) 的运算结果。

示例:计算并显示 \(-9 \oplus 57\)

# Once
scoreboard players set $bitwise.xor.a bs.in -9
scoreboard players set $bitwise.xor.b bs.in 57
function #bs.bitwise:xor

# See the result
tellraw @a [{"text":"-9 ^ 57 = ","color":"#e0c7ff"},{"score":{"name":"$bitwise.xor","objective":"bs.out"}}]
#bs.bitwise:not

计算一个数字的按位非运算。

输入:

分数 $bitwise.not.n bs.in:要取反的数字。

输出:

返回值 | 分数 $bitwise.not bs.out:按位非的运算结果。

示例:计算并显示 \(\lnot 452\)

# Once
scoreboard players set $bitwise.not.n bs.in 452
function #bs.bitwise:not

# See the result
tellraw @a [{"text":"~452 = ","color":"#e0c7ff"},{"score":{"name":"$bitwise.not","objective":"bs.out"}}]

制作人员:Aksiome、theogiraudet


💬 这对你有帮助吗?

欢迎在下方留下你的问题和反馈!