2016-04-22 36 views
-3

我試圖將一些銀行代碼爲JavaScript,但遇到顛簸,因爲我不知道是什麼一些斯威夫特這些運營商的做,所以到這是類似於JavaScript表單我無法將它轉換。下面是代碼:斯威夫特運營商解釋

private var portMasks = [UInt8](count: 3, repeatedValue: 0) 

var newMask = UInt8(newState.rawValue * Int(powf(2, Float(pinIndex)))) 
portMasks[Int(port)] &= ~(1 << pinIndex) //prep the saved mask by zeroing this pin's corresponding bit 
newMask |= portMasks[Int(port)] //merge with saved port state 
portMasks[Int(port)] = newMask 
data1 = newMask<<1; data1 >>= 1 //remove MSB 
data2 = newMask >> 7 //use data1's MSB as data2's LSB 

pinIndexport已經被定義,都UInt8

我不是在尋找的實際轉換 - 我可以做我自己。只需從數學/程序化的角度尋找這些行中每一行的解釋即可。

+4

所有Swift運算符都詳細記載在參考中:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27 -ID28。 –

+0

感謝您的鏈接!它有一些操作符,但缺少'>> ='和'| =' –

+1

在至少瀏覽文檔之前,您沒有發佈有關Swift的問題,是嗎? :) –

回答

0

開始一個新的平臺,並複製這裏面:

var port: UInt8 = 1 
var pinIndex: UInt8 = 2 
var newState: Int = 23 

var portMasks = [UInt8](count: 3, repeatedValue: 0) 

var newMask = UInt8(newState * Int(powf(2, Float(pinIndex)))) 
portMasks[Int(port)] &= ~(1 << pinIndex) //prep the saved mask by zeroing this pin's corresponding bit 
newMask |= portMasks[Int(port)] //merge with saved port state 
portMasks[Int(port)] = newMask 
var data1 = newMask<<1; data1 >>= 1 //remove MSB 
var data2 = newMask >> 7 //use data1's MSB as data2's LSB 

你會看到右側的影響。

<< >> move bits by that amount 
| logical or 
& logical and 
~ logical negation 

認爲這就是所有這裏使用的。基本上,您的代碼正在操縱單個位(由1 < bit位置引用),並打開或關閉它們。

這兩行的意思是一樣的:

variable OPERATOR= something 
variable = variable OPERATOR something 

會建議身邊一個小玩,然後回來與具體問題。

+0

感謝您的支持!讓我去檢查一下 –