如何使用Swift移位?

use*_*440 5 swift

在Objective-C中,代码是

uint16_t majorBytes;
[data getBytes:&majorBytes range:majorRange];
uint16_t majorBytesBig = (majorBytes >> 8) | (majorBytes << 8);
Run Code Online (Sandbox Code Playgroud)

在斯威夫特

    var majorBytes: CConstPointer<UInt16> = nil

    data.getBytes(&majorBytes, range: majorRange)
Run Code Online (Sandbox Code Playgroud)

aobut majorBytesBig怎么样?

wot*_*pal 8

Bit-Shifting-Syntax没有从ObjC变为Swift.只需查看Swift-Book中的Advanced Operators一章,即可深入了解这里发生的事情.

// as binary: 0000 0001 1010 0101 (421)
let majorBytes: UInt16 = 421

// as binary: 1010 0101 0000 0000 (42240)
let majorBytesShiftedLeft: UInt16 = (majorBytes << 8)

// as binary: 0000 0000 0000 0001 (1)
let majorBytesShiftedRight: UInt16 = (majorBytes >> 8)

// as binary: 1010 0101 0000 0001 (42241)
let majorBytesBig = majorBytesShiftedRight | majorBytesShiftedLeft
Run Code Online (Sandbox Code Playgroud)