Skip to main content

位运算表达式化简器

安装

pip install BitwiseExpressionSimplifier

更新

pip install --upgrade BitwiseExpressionSimplifier

功能特性

基本用法

将表达式中未知数的名称集合、表达式、位长度传入toNOperation方法,可返回由NOperation类派生的NExpression类、NNumber类 或NKnownNumber类。

NOperation类有bitwise属性,可供查看每位的最简表达式。

from BitwiseExpressionSimplifier import toNOperation


result = toNOperation("(y^(x>>2))&0xF0F", 16, {'x', 'y'}).bitwise
# result[0] = y[0] ^ x[2]
# result[1] = y[1] ^ x[3]
# result[2] = y[2] ^ x[4]
# result[3] = y[3] ^ x[5]
# result[4] = 0
# result[5] = 0
# result[6] = 0
# result[7] = 0
# result[8] = y[8] ^ x[10]
# result[9] = y[9] ^ x[11]
# result[10] = y[10] ^ x[12]
# result[11] = y[11] ^ x[13]
# result[12] = 0
# result[13] = 0
# result[14] = 0
# result[15] = 0

表达式中无未知数时,表达式中未知数的名称集合请置为set()或者不传参,而不是{}。

表达式支持自动判定运算优先级,支持任意长度的空格填充,支持多层括号的嵌套。

位长度为运算时的溢出边界,用于模拟程序中的真实溢出情况,如在int环境下,其值应当为32。

高阶玩法

可以将NOperation类的实例作为未知数传入新表达式参与化简。

from BitwiseExpressionSimplifier import toNOperation


# 每字节中,新x高四位为原x高四位,新x低四位为原y高四位,新y高四位为原x低四位,新y低四位为原y低四位
base = toNOperation("(x^(y>>4))&0xf0f0f0f", 32, {'x', 'y'})
x = toNOperation("x^base", 32, {'x'}, {'base': base}).bitwise
y = toNOperation("y^(base<<4)", 32, {'y'}, {'base': base}).bitwise
# x[0] = y[4]
# x[1] = y[5]
# x[2] = y[6]
# x[3] = y[7]
# x[4] = x[4]
# x[5] = x[5]
# x[6] = x[6]
# x[7] = x[7]
# x[8] = y[12]
# x[9] = y[13]
# x[10] = y[14]
# x[11] = y[15]
# x[12] = x[12]
# x[13] = x[13]
# x[14] = x[14]
# x[15] = x[15]
# x[16] = y[20]
# x[17] = y[21]
# x[18] = y[22]
# x[19] = y[23]
# x[20] = x[20]
# x[21] = x[21]
# x[22] = x[22]
# x[23] = x[23]
# x[24] = y[28]
# x[25] = y[29]
# x[26] = y[30]
# x[27] = y[31]
# x[28] = x[28]
# x[29] = x[29]
# x[30] = x[30]
# x[31] = x[31]

# y[0] = y[0]
# y[1] = y[1]
# y[2] = y[2]
# y[3] = y[3]
# y[4] = x[0]
# y[5] = x[1]
# y[6] = x[2]
# y[7] = x[3]
# y[8] = y[8]
# y[9] = y[9]
# y[10] = y[10]
# y[11] = y[11]
# y[12] = x[8]
# y[13] = x[9]
# y[14] = x[10]
# y[15] = x[11]
# y[16] = y[16]
# y[17] = y[17]
# y[18] = y[18]
# y[19] = y[19]
# y[20] = x[16]
# y[21] = x[17]
# y[22] = x[18]
# y[23] = x[19]
# y[24] = y[24]
# y[25] = y[25]
# y[26] = y[26]
# y[27] = y[27]
# y[28] = x[24]
# y[29] = x[25]
# y[30] = x[26]
# y[31] = x[27]

支持化简的情况

  1. 位移操作

    缺省溢出位将直接置0

  2. 1 & x or x & 1

    =x

  3. 0 & x or x & 0

    =0

  4. x & x

    =x

  5. x & ~x

    =0

  6. (x & y & ...) & x or x & (x & y & ...)

    =x & y & ...

  7. (x & y & ...) & ~x or ~x & (x & y & ...)

    =0

  8. (x & y & ...) & (y & z & ...)

    =(x & y & ...) & (z & ...)

  9. (x & y & ...) & (~y & z & ...) or (x & ~y & ...) & (y & z & ...)

    =0

  10. 1 | x or x | 1

    =1

  11. 0 | x or x | 0

    =x

  12. x | x

    =x

  13. x | ~x

    =1

  14. (x | y | ...) | x or x | (x | y | ...)

    =x | y | ...

  15. (x | y | ...) | ~x or ~x | (x | y | ...)

    =1

  16. (x | y | ...) | (y | z | ...)

    =(x | y | ...) | (z | ...)

  17. (x | y | ...) | (~y | z | ...) or (x | ~y | ...) | (y | z | ...)

    =1

  18. 0 ^ x or x ^ 0

    =x

  19. 1 ^ x or x ^ 1

    =~x

  20. x ^ x

    =0

  21. x ^ ~x

    =1

  22. (x ^ y ^ ...) ^ x or x ^ (x ^ y ^ ...)

    =y ^ ...

  23. (x ^ y ^ ...) ^ ~x or ~x ^ (x ^ y ^ ...)

    =~(y ^ ...)

  24. (x ^ y ^ ...) ^ (y ^ z ^ ...)

    =(x ^ ...) ^ (z ^ ...)

  25. (x ^ y ^ ...) ^ (~y ^ z ^ ...) or (x ^ ~y ^ ...) ^ (y ^ z ^ ...)

    =~((x ^ ...) ^ ( z ^ ...))

  26. 0 ⊙ x or x ⊙ 0

    =x

  27. 1 ⊙ x or x ⊙ 1

    =~x

  28. x ⊙ x

    =0

  29. x ⊙ ~x

    =1

  30. (x ⊙ y ⊙ ...) ⊙ x or x ⊙ (x ⊙ y ⊙ ...)

    =y ⊙ ...

  31. (x ⊙ y ⊙ ...) ⊙ ~x or ~x ⊙ (x ⊙ y ⊙ ...)

    =~(y ⊙ ...)

  32. (x ⊙ y ⊙ ...) ⊙ (y ⊙ z ⊙ ...)

    =(x ⊙ ...) ⊙ (z ⊙ ...)

  33. (x ⊙ y ⊙ ...) ⊙ (~y ⊙ z ⊙ ...) or (x ⊙ ~y ⊙ ...) ⊙ (y ⊙ z ⊙ ...)

    =~((x ⊙ ...) ⊙ ( z ⊙ ...))

最后

  • 有任何错误、BUG、未化简的情况,请务必发起issue告知开发者。
  • 有任何建议、意见,如认为合理,同样请务必发起issue告知开发者。

Metadata

Release files for BitwiseExpressionSimplifier 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for BitwiseExpressionSimplifier 1.0.3
File Size Uploaded Provenance Actions
BitwiseExpressionSimplifier-1.0.3.tar.gz 8.1 kB Without provenance Details for BitwiseExpressionSimplifier-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for BitwiseExpressionSimplifier 1.0.3
File Interpreter ABI Platform Actions
BitwiseExpressionSimplifier-1.0.3-py3-none-any.whl Python 3 none any Details for BitwiseExpressionSimplifier-1.0.3-py3-none-any.whl

Total release size: 29.4 kB

Release files / BitwiseExpressionSimplifier-1.0.3.tar.gz

Download URL BitwiseExpressionSimplifier-1.0.3.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8a569f0bfb2f08b45c6bbf0809db02b727c540ecb44b850bd96519793e897b7d
BLAKE2b-256 checksum
How to use checksums
8621cfe760db90cd00da3d0b1cbda7b7bbc60a8eff4bec837adfcffdbed33248
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.8

Release files / BitwiseExpressionSimplifier-1.0.3-py3-none-any.whl

Download URL BitwiseExpressionSimplifier-1.0.3-py3-none-any.whl
Size 21.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
713cdf1f42591940ac6307d144e7c2f940c0835645a66eaa2f5f637739b067de
BLAKE2b-256 checksum
How to use checksums
b31d4bf61bdb1efff2e880db1aefc92f0fa338499741b32cba53c6339531552f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.8

Security for BitwiseExpressionSimplifier 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page