Bitwise right shift >> in Objective-C

此生再无相见时 提交于 2019-12-10 13:49:55

问题


I have a variable (unsigned int) part_1.

If I do this: NSLog(@"%u %08x", part_1, part_1); (print unsigned value, and hex value) it outputs:

2063597568 7b000000

(only first two will have values).

I want to convert this to

0000007b

So i've tried doing unsigned int part_1b = part_1 >> 6 (and lots of variations)

But this outputs:

32243712 01ec0000

Where am i going wrong?


回答1:


You want to shift by 6*4 = 24 bits, not just 6 bits. Each '0' in the hex printf represents 4 bits.

unsigned int part_1b = part_1 >> 24;
                                 ^^


来源:https://stackoverflow.com/questions/19794519/bitwise-right-shift-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!