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 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. You can build a lookup table, with 32 elements: 0, 1, 2, 2, 3, etc. Paul R This is effectively the same operation as counting he number of leading 0s. Some CPUs have an instruction for this, otherwise you can use tricks such as