Getting the Leftmost Bit
问题 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 回答1: 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. 回答2: You can build a lookup table, with 32 elements: 0, 1, 2, 2, 3, etc. 回答3: This is effectively the same operation as