2014-10-03 89 views
1

這是我的測試函數。GoLang的Int溢出

if -1 != cmp(2<<32, keys[2].Distance(keys[5])) { 
     t.Errorf("2<<32 should be smaller") 
    } 

它導致folllowing錯誤

不斷8589934592溢出詮釋

是否有可能使一個32位系統上這項工作?

編輯:還這是比較關鍵

距離函數
// Distance returns the distance metric in this key space 
func (s *xorKeySpace) Distance(k1, k2 Key) *big.Int { 
    // XOR the keys 
    k3 := XOR(k1.Bytes, k2.Bytes) 

    // interpret it as an integer 
    dist := big.NewInt(0).SetBytes(k3) 
    return dist 
} 
+1

你確定你使用的golang版本是64位嗎?根據文檔上的64位去int應該是64位 – 2014-10-03 19:54:02

+0

我剛剛檢查了一個64位版本去,並沒有問題,2 << 32作爲int – 2014-10-03 19:56:35

+0

@sourcey運行'去版本',輸出是什麼你的機器。 – nos 2014-10-03 19:58:40

回答

1

確保你在一個64位INT的工作,最好的辦法是使用uint64

type Key int64 // or uint64 

假設,以確保尺寸鍵被定義爲int,否則將所有功能簽名從int更改爲int64