Not understandable code

 praveenraj .s.r · April 30, 2015  I read a easy method to find power of two.. and here is the one line code they gave:#define ISPOWOF2( n )     ( ! ( n & ( n-1 ) )and that's it. I don't know how to use this. Anybody here help me please..

Replies

 praveenraj .s.r · May 3, 2015 yeaaa!! He is right.. But I don't know much about bits operation praveenraj .s.r · May 3, 2015 yeaaa!! He is right.. But I don't know much about bits operation praveenraj .s.r · May 3, 2015 yeaaa!! He is right.. But I don't know much about bits operation praveenraj .s.r · May 3, 2015 yeaaa!! He is right.. But I don't know much about bits operation zhang yida · May 2, 2015 well I think you can understand it like const ,it mean you have define a ISPOWOF, the value is 2( n ) ( ! ( n & ( n-1 ) ) ,you have to learn bits operation and know what's the 2( n ) ( ! ( n & ( n-1 ) ) mean. I think Arjun Patel is right. Dol Lod · April 30, 2015 The & operator is doing a bitwise and between 2 32 bit numbers, n and n-1. If they have any bits in common that are ones, that will be reversed by the !. This whole expression is defined as a macro taking in one number.What this is saying is that the number one less than it should have not bits in common with the previous numbe other than the sign bit potentially. The reason why this expression works is that if it truly is a power of 2, only 1 bit should be a 1 so if any bits match up with the previous number, that would be impossible if it was truly a power of 2. FYI, you are missing a parenthases at the end. I also did test this expression out for confirmation and it did actually work.
• 1

C

129,793 followers