在一次求职面试中,我曾经被要求计算位向量结构中的正数(即设置为"1")位数(如无符号整数或长整数).我的解决方案在C#中非常简单:
int CountBits(uint input)
{
int reply = 0;
uint dirac = 1;
while(input != 0)
{
if ((input & dirac) > 0) reply++;
input &= ~dirac;
dirac<<=1;
}
return reply;
}
Run Code Online (Sandbox Code Playgroud)
然后我被要求在不使用任何轮班的情况下解决任务:既不明确(如"<<"或">>")也不隐含(如乘以2).使用潜在的2行(如0,1,2,4,8,16等)的"暴力"解决方案也不会这样做.
有人知道这样的算法吗?
据我所知,它应该是一种或多或少的通用算法,它不依赖于输入位向量的大小.允许所有其他按位运算和任何数学函数.
Perl非常适合做位串/向量.设置位非常简单
vec($bit_string, 123, 1) = 1;
Run Code Online (Sandbox Code Playgroud)
获取设置位的计数很快
$count = unpack("%32b*", $bit_string);
Run Code Online (Sandbox Code Playgroud)
但如果你设置在2_147_483_639之上,你的计数将默默地变为零而没有任何明显的警告或错误.
有没有办法解决?
以下代码演示了此问题
#!/usr/bin/env perl
# create a string to use as our bit vector
my $bit_string = undef;
# set bits a position 10 and 2_000_000_000
# and the apparently last valid integer position 2_147_483_639
vec($bit_string, 10, 1) = 1;
vec($bit_string, 2_000_000_000, 1) = 1;
vec($bit_string, 2_147_483_639, 1) = 1;
# get a count of the bits which are set
my $bit_count = unpack("%32b*", $bit_string);
print("Bits set in …Run Code Online (Sandbox Code Playgroud) 我正在做一个项目,在某个时刻我需要显示一个月的时间,这些天仍然可用。有一个功能可以计算哪些日期可用。我的同事说:“哦,我们知道,您应该返回a BitVector32。这是使用布尔值列表时最有效的方法。” 我会用一个List<bool>或类似的东西。BitVector32在您实际使用位时,对我而言,A 似乎是低级内容。
所以,问题是。您是否应该BitVector32在需要少于32个布尔值的布尔值列表时使用Every,还是仅将其用于低级内容?
我们都可能知道C++ 98 vector<bool>专门化将布尔值存储为位而不是bool变量.vector<bool>的元素是不可寻址的,因为C++没有指针和对位的引用,是否有解决方法,任何明显的陷阱(我似乎都没有注意到),甚至尝试这样做是否实用?
我是z3py的新手,正在使用Python中的Z3 API,但无法弄清楚如何定义一个bitvectors数组.
我想要的东西:
DOT__mem[16] = BitVec('DOT__mem[16]', 8)
Run Code Online (Sandbox Code Playgroud)
但是这种语法不起作用,即使在本教程的练习面板上也是如此.
有人可以帮助正确的语法吗?
:变量 x 被定义为 int 排序方式 (declare-const x Int)
有什么方法可以将 x 转换为位向量排序吗?因为有时x涉及到int理论无法处理的&、|、^等位运算。
我不想一开始就将变量 x 定义为位向量,因为我认为 int 理论支持的运算(例如,+、-、*、/)除了位运算之外,运行速度比位向量支持的运算快得多。
所以实际上,我想根据需要将 int 排序转换为位向量排序,反之亦然。
我在理解这两个函数中的位逻辑时遇到麻烦。
我不知道为什么我们要检查条件(bitVector&mask)== 0。
另外,为什么在满足条件时我们将bitVector与掩码进行“或”运算,否则将bitVector与〜mask进行“与”运算呢?
为什么要有这样一个属性,使得人们可以“通过从整数中减去一位并将其与原始整数进行“与”运算,来检查是否已正确设置了一位?
完整代码在这里。
/* Toggle the ith bit in the integer. */
public static int toggle(int bitVector, int index) {
if (index < 0) return bitVector;
int mask = 1 << index;
if ((bitVector & mask) == 0) {
bitVector |= mask;
} else {
bitVector &= ~mask;
}
return bitVector;
}
/* Check that exactly one bit is set by subtracting one from the
* integer and ANDing it with the original integer. */ …Run Code Online (Sandbox Code Playgroud) 我是 Rust 的初学者。我正在尝试使用BitVec库来表示位数组。我开始通过附加 0 或 1 的序列来使用它,但我在这样做时遇到了一些问题。当我附加一个 x 0序列然后是一个 y 1 序列时,它所做的是附加 x+y 零。请注意,如果我之前只附加 1 而未附加 0,则它可以工作。这是我的代码:
extern crate bit_vec;
use bit_vec::BitVec;
fn main(){
let mut bits = BitVec::new(); // creates an empty array of bits
append_zero(&mut bits);
append_one(&mut bits);
append_zero(&mut bits);
append_one(&mut bits);
append_one(&mut bits); // everything works perfectly till here
append_n_ones(&mut bits, 2); // this works
append_n_zeroes(&mut bits, 3); // this too
append_n_ones(&mut bits, 2); // this appends 2 zeroes instead!
println!("{:?}", bits);
} …Run Code Online (Sandbox Code Playgroud) 在Python,什么是最好的数据结构n的比特(这里n为约10000),其上进行通常的二进制运算(&,|,^与其它这样的数据结构)是快?
我正在尝试编写一个辅助函数,它将把位索引数组转换为符合 OptionSet 的类。
func getOptionSet<T: OptionSet>(bitIndexes: [Int64]) -> T {
var result: Int64 = 0
for index in bitIndexes {
result |= 1 << index
}
return T(rawValue: result) // error
}
Run Code Online (Sandbox Code Playgroud)
这无法编译:
Cannot invoke initializer for type 'T' with an argument list of type '(rawValue: Int64)'
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用 RawValue:
func getOptionSet<T: OptionSet>(bitIndexes: [T.RawValue]) {
var result = T.RawValue() // error
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
Cannot invoke value of type 'T.RawValue.Type' with argument list '()'
Run Code Online (Sandbox Code Playgroud)
这可以做到吗?我需要对 T 添加额外的约束吗?
我知道可以重写这个函数以使用具体类型,但如果可能的话我想保持它的通用性。