C 到 Swift 按位运算

标签 c swift bitwise-operators

我在 C 中有这些函数(来自 Cactus Kev 的扑克评估器):

unsigned find_fast(unsigned u)
{
    unsigned a, b, r;
    u += 0xe91aaa35;
    u ^= u >> 16;
    u += u << 8;
    u ^= u >> 4;
    b  = (u >> 8) & 0x1ff;
    a  = (u + (u << 2)) >> 19;
    r  = a ^ hash_adjust[b];
    return r;
}

int eval_5hand_fast(int c1, int c2, int c3, int c4, int c5)
{
    int q = (c1 | c2 | c3 | c4 | c5) >> 16;
    short s;
    if (c1 & c2 & c3 & c4 & c5 & 0xf000)
        return flushes[q]; 
    if ((s = unique5[q]))
        return s;          
    return hash_values[find_fast((c1 & 0xff) * (c2 & 0xff) * (c3 & 0xff) * (c4 & 0xff) * (c5 & 0xff))];
}

并希望将它们转换为 Swift:

func eval_5hand_fast(c1: Int, c2: Int, c3: Int, c4: Int, c5: Int) -> Int {

    var q: Int = (c1 | c2 | c3 | c4 | c5) >> 16
    var s: Int8
    if c1 & c2 & c3 & c4 & c5 & 0xf000 {
        return flushes[q]
    }
    if (s = unique5[q]) {
        return s
    }
    return hash_values[find_fast((c1 & 0xff) * (c2 & 0xff) * (c3 & 0xff) * (c4 & 0xff) * (c5 & 0xff))]
}

func find_fast(u: UInt) -> UInt {
    var a, b, r: UInt
    u += 0xe91aaa35
    u ^= u >> 16
    u += u << 8
    u ^= u >> 4
    b  = (u >> 8) & 0x1ff
    a  = (u + (u << 2)) >> 19
    r  = a ^ hash_adjust[b]
    return r;
}

我只是在学习 Swift,并没有真正对按位运算做过很多事情,所以请多多包涵。我试图自己解决这些问题,但无济于事。我在使用 Swift 时遇到的错误包括:

  1. if c1 & c2 & c3 & c4 & c5 & 0xf000
    // ERROR: Type 'Int' does not conform to protocol 'BooleanType'
  2. if (s = unique5[q])
    // ERROR: Type '()' does not conform to protocol 'BooleanType'
  3. return hash_values[find_fast((c1 & 0xff) * (c2 & 0xff) * (c3 & 0xff) * (c4 & 0xff) * (c5 & 0xff))]
    // ERROR: Cannot find an overload for '*' that accepts the supplied arguments
  4. u += 0xe91aaa35
    // ERROR: Cannot invoke '+=' with an argument list of type '(UInt, IntegerLiteralConvertible)'
  5. u ^= u >> 16
    // ERROR: Cannot invoke '>>' with an argument list of type '(UInt, $T5)'
  6. u += u << 8
    // ERROR: Cannot invoke '+=' with an argument list of type '(UInt, $T5)'
    
  7. u ^= u >> 4
    // ERROR: Cannot invoke '>>' with an argument list of type '(UInt, $T5)'

我得到第一个错误(它不是 bool 值),但是我不确定如何解决它,因为我不完全确定 C 版本在问什么,因为我不太熟悉使用位和掩码。其他错误我真的不知道该怎么办。

最佳答案

当谈到整数时,任何非 0 的东西在 C 中都被认为是 true。Swift 需要一个 bool 值,所以你必须添加 != 0。例如:

C:     if c1 & c2 & c3 & c4 & c5 & 0xf000
Swift: if c1 & c2 & c3 & c4 & c5 & 0xf000 != 0

C:     if (s = unique5[q])
Swift: if let s = unique5[q] where s != 0

试试这个:

func eval_5hand_fast(c1: Int, c2: Int, c3: Int, c4: Int, c5: Int) -> Int {

    var q: Int = (c1 | c2 | c3 | c4 | c5) >> 16
    var s: Int8
    if c1 & c2 & c3 & c4 & c5 & 0xf000 != 0 {
        return flushes[q]
    }
    if let s = unique5[q] where s != 0 {
        return s
    }
    return hash_values[find_fast((c1 & 0xff) * (c2 & 0xff) * (c3 & 0xff) * (c4 & 0xff) * (c5 & 0xff))]
}

func find_fast(var u: UInt) -> UInt {
    var a, b, r: UInt
    u += 0xe91aaa35
    u ^= u >> 16
    u += u << 8
    u ^= u >> 4
    b  = (u >> 8) & 0x1ff
    a  = (u + (u << 2)) >> 19
    r  = a ^ hash_adjust[b]
    return r;
}

关于C 到 Swift 按位运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32292413/

相关文章:

c - 将 .txt 读入矩阵

ios - Interface Builder 文件中的未知类 _TtC8<appName>20MasterViewController

c - 如何判断三个数是否相等

python - Python 的按位求补运算符 (~ 代字号) 是如何工作的?

c++ - C/C++ 中的大数据集表示

检查从 aaa..a 到 zzz..z 的每个 "word"

检查值是否在音叉反汇编中

ios - 由于滞后,TableViewController 搜索结果无法正确加载

ios - 如何在 Swift 中将 JSON 写入 Realm

python - 使用 'str' 时,我的按位程序出现 'int' 和 'input' TypeError