0

I have problem with For which types of number does while loop iterates for?(I know the function of &)while (a & a-1).

Bitwise operation

26th Jul 2018, 12:35 PM
Shakkir Moulana
Shakkir Moulana - avatar
7 Respuestas
0
in your specific case while(a & a-1) should work as long as "a" is a number (i forget if bitwise operators work on fixed/floating-point numbers)
26th Jul 2018, 7:25 PM
hinanawi
hinanawi - avatar
0
It won't work for a=8
27th Jul 2018, 3:50 AM
Shakkir Moulana
Shakkir Moulana - avatar
0
Shakkir Moulana the code will work it's just that the while loop won't ever execute because 8 (0000 1000) & 7 (0000 0111) = 0 (0000 0000) which equates to false apologies if i misunderstood the question i thought it was about the code working not the loop
27th Jul 2018, 6:24 AM
hinanawi
hinanawi - avatar
0
Can you mention for which types of numbers would the loop iterates
27th Jul 2018, 7:10 AM
Shakkir Moulana
Shakkir Moulana - avatar
0
Shakkir Moulana the loop would run under numbers which keep at least one "1" in the same place when they are subtracted from by 1. examples: 6 (0000 0110) & 5 (0000 0101) = 4 (0000 0100) which equates to true iirc 7 (0000 0111) & 6 (0000 0110) = 6 (0000 0110) which is also true
27th Jul 2018, 7:36 AM
hinanawi
hinanawi - avatar
0
is there any pattern while observing decimal numbers (not by observing binary)
27th Jul 2018, 10:25 AM
Shakkir Moulana
Shakkir Moulana - avatar
0
Shakkir Moulana nope, as these are bitwise operators they strictly work with binary representations of numbers
27th Jul 2018, 11:20 AM
hinanawi
hinanawi - avatar