how to find leftmost 1-bit in C++ ?
Разница между en1 и en2, 3 символ(ов) изменены
how to find leftmost 1-bit in C++ ?↵

in other word I want to find biggest k so that 2^k <= N for some given number N↵

for example if N=10 then k=3↵

I want an O(1) time and memory solution and also without using built-in functions that may not compile in ALL C++ compilers.↵

thank you in advance.↵

plz tell 
me this is correct or not ?↵

-------------------------------------------↵
int highestOneBit(int i) {↵
    i |= (i >>  1);↵
    i |= (i >>  2);↵
    i |= (i >>  4);↵
    i |= (i >>  8);↵
    i |= (i >> 16);↵
    return i &mdash; (i >> 1);↵
}

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский bajuddin15 2021-09-25 19:12:00 3 Tiny change: 'nplz tell this is c' -> 'nplz tell me this is c'
en1 Английский bajuddin15 2021-09-25 19:11:08 590 Initial revision (published)