Objective c:整数的二元运算?
我对以下代码片段有基本逻辑问题:
1 uint64_t RMTileKey(RMTile tile)
2 {
3 uint64_t zoom = (uint64_t) tile.zoom & 0xFFLL; // 8bits, 256 levels
4 uint64_t x = (uint64_t) tile.x & 0xFFFFFFFLL; // 28 bits
5 uint64_t y = (uint64_t) tile.y & 0xFFFFFFFLL; // 28 bits
6
7 uint64_t key = (zoom << 56) | (x << 28) | (y << 0);
8
9 return key;
10 }
返回值键是一个无符号整数。 我现在很困惑,因为我不明白第 3(4,5) 行发生了什么。运营商 &用我的 uint64_t 做什么。我猜它正在转换为十六进制值? 然后在第 7 行中,我从位 0 移动到位 27(28 到 56 ...)并合并这些基于十六进制的数字?
I have BASIC logical question about the following code snippet:
1 uint64_t RMTileKey(RMTile tile)
2 {
3 uint64_t zoom = (uint64_t) tile.zoom & 0xFFLL; // 8bits, 256 levels
4 uint64_t x = (uint64_t) tile.x & 0xFFFFFFFLL; // 28 bits
5 uint64_t y = (uint64_t) tile.y & 0xFFFFFFFLL; // 28 bits
6
7 uint64_t key = (zoom << 56) | (x << 28) | (y << 0);
8
9 return key;
10 }
The return value key is an unsigned integer.
I am very confused now, because i dont understand what is happening in line 3(4,5). The operator & does what with my uint64_t. I guess it is converting to a hex-value ?
And then in line 7 i shift from bit 0 to 27 (28 to 56 ...) and merge theses hex-based numbers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第 3 行:
zoom
包含除 0 到 7 之外的所有位 清除:0000...000zzzzzzzz
8 位第 4 行:
x
包含所有位0 到 27 除外:0000...000xxxx...xxxx
; 28 位第 5 行:
y
清除除 0 到 27 之外的所有位:0000...000yyyy...yyyy
; 28 位第 7 行:这些位被重新排列以形成单个 64 位值:
line 3:
zoom
has all the bits except 0 to 7 clear:0000...000zzzzzzzz
8 bitsline 4:
x
has all the bits except 0 to 27 clear:0000...000xxxx...xxxx
; 28 bitsline 5:
y
has all the bits except 0 to 27 clear:0000...000yyyy...yyyy
; 28 bitsline 7: the bits are rearranged to make a single 64-bit value:
第一的 -
&
是二元 AND 运算符。|
是二元或运算符。tile.zoom
的 LSB 分配给zoom
(模 0x100)tile.x
的 28 个最低有效位code> 到x
(模 0x10000000)第 5 行作为第 4 行。
第 7 行将通过将上述所有变量放入
key
的不同位(偏移量)来构造键变量。First -
&
is the binary AND operator.|
is the binary OR operator.tile.zoom
tozoom
(which is modulu 0x100)tile.x
tox
(which is modulu 0x10000000)Line 5 as line 4.
Line 7 will construct the key variable by putting all the above variable in different bits(offsets) of
key
.