0
我想用我的类来创建一些属性的一个唯一的标识符:添加hashValues一起使用位异或运算符或简单地添加在一起
class Foo {
var name = "John"
var array = ["string1", "string2"]
var check: Bool { return name.isEmpty }
var xor: Int { return name.hashValue^check.hashValue }
var add: Int { return name.hashValue + check.hashValue }
}
let test = Foo()
print(test.xor) // prints 4799450059756749538
print(test.add) // prints 4799450059756749538
没有问题的工作。
但是在阅读一些教程时,一些站点使用运算符^
将散列值“添加”在一起。
我的问题是,如果我使用运算符^
来计算identifier
,它是否有任何(负面)影响?因为当我计算数组的哈希值,我不能使用运营商+
,我被迫使用^
,而不是(除非我使用Int32
/Int64
代替):
var identifier: Int { return name.hashValue^array.map { $0.hashValue }.reduce(0) { $0^$1 }^check.hashValue }
而且我已经看到人们在处理散列值时使用按位左移和右移操作符。这有什么特别的理由吗?