z3 - 使用 Datalog 输入格式在 Z3 中的 BitVec 中计数
问题描述
是否有一种紧凑的方法可以使用 Datalog 输入格式计算 Z3 中 BitVec 中设置为 1 的位数?
$ z3 -h # most of the lines below omited for clarity
Input format:
-dl use parser for Datalog input format.
目前接受的这个问题的答案:Count one in Z3 in BitVec with SMT 2 input format指出在 SMT 2 输入格式中没有很好的方法。
这个问题目前接受的答案:Sum of all the bits in a Bit Vector of Z3展示了如何在 Python 中执行此操作。
解决方案
对于32 bit
向量,您可以尝试将以下伪代码转换为 SMT:
v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
这被称为Bit Twiddling Hack并且也被张贴在这里。
推荐阅读
- jquery - 使用 JSON 缓存后保持选中单选按钮
- python - 在 Python 中实现模块热重载的最佳方法是什么?
- python - 实时 matplotlib 绘图保持轴值(x 轴上只有 10 个)
- java - 如何在线程中有一个有效的循环
- javascript - 如何在javascript中将切换功能转换为模块模式?
- ios - 应用程序重新启动时如何恢复计时器?斯威夫特 4 iOS 11
- python - 我应该如何通过网络(wifi)访问连接到笔记本电脑(python)中树莓派的网络摄像头?
- python - 在循环键更新值时,所有键值都在字典中更新?
- tensorflow.js - 如何将一维张量转换为常规 JavaScript 数组?
- python - 张量流中的采样