获取最左边的位 [英] Getting the Leftmost Bit

查看:108
本文介绍了获取最左边的位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个5位整数,我的工作。是否有在Objective-C本机的功能,可以让我知道这一点是最左边的?

即。我有01001,它将返回8的位置。

感谢


解决方案

  NSInteger的价值= 9;
NSInteger的转变= 1;
对于(NSInteger的位=值;位大于1​​;位=价值>> ++移);
NSInteger的leftmostbit = 1<<转移;

对于工作位的每一个数字。

I have a 5 bit integer that I'm working with. Is there a native function in Objective-C that will let me know which bit is the leftmost?

i.e. I have 01001, it would return 8 or the position.

Thanks

解决方案

NSInteger value = 9;
NSInteger shift = 1;
for(NSInteger bit = value; bit > 1; bit = value >> ++shift);
NSInteger leftmostbit = 1 << shift;

Works for every number of bits.

这篇关于获取最左边的位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆