I have tried to understand the time complexity of the below code. When I try to calculate the timecomplexity myself it is coming out to be o(n). Because the "NO_OF_BITS" is always same for any int and therefore the loop doesn't increase/decrease with the input. I was not sure about the bitwise operations that are executed inside the loop. Can anyone help me understand/calculate the time complexity of this code.
unsigned int reverseBits(unsigned int num)
{
int NO_OF_BITS = sizeof(num) * 8;
int reverse_num = 0;
int i;
for (i = 0; i < NO_OF_BITS; i++)
{
if((num & (1 << i)))
reverse_num |= 1 << ((NO_OF_BITS - 1) - i);
}
return reverse_num;
}
(num & (1 << i))
is executed N times. All bits are looked at. Who said this is O(log N)? – OneCricketeerN
as innum
, notNO_OF_BITS
. – Eric DuminilO(logN)
? – Lasse V. Karlsen