Detect Power of two

Started by
21 comments, last by dyerseve 18 years, 7 months ago
Quote:Original post by Eelco
i dont know how your processor works, but my signed integer types always have been of the 0..127,-128..-1 kind. i dont see how counting bits would work there.

actually, thinking about it, id be curious to know how a signed number could ever possibly hope to be an interger power of two anyway... not going to happen, so its a nonissue.
It can't be, but the lowest signed value only has one bit set (the sign bit) and is thus falsely reported as a power of two.
Advertisement
Quote:Original post by doynax
Quote:Original post by Eelco
i dont know how your processor works, but my signed integer types always have been of the 0..127,-128..-1 kind. i dont see how counting bits would work there.

actually, thinking about it, id be curious to know how a signed number could ever possibly hope to be an interger power of two anyway... not going to happen, so its a nonissue.
It can't be, but the lowest signed value only has one bit set (the sign bit) and is thus falsely reported as a power of two.


what do you mean by lowest signed value? -1 has all bits set, and thus is not reported as a power of two, and -128 only has the first bit set, and ignoring the sign, is thus reported correctly.
Quote:Original post by Eelco
Quote:Original post by doynax
Quote:Original post by Eelco
i dont know how your processor works, but my signed integer types always have been of the 0..127,-128..-1 kind. i dont see how counting bits would work there.

actually, thinking about it, id be curious to know how a signed number could ever possibly hope to be an interger power of two anyway... not going to happen, so its a nonissue.
It can't be, but the lowest signed value only has one bit set (the sign bit) and is thus falsely reported as a power of two.


what do you mean by lowest signed value? -1 has all bits set, and thus is not reported as a power of two, and -128 only has the first bit set, and ignoring the sign, is thus reported correctly.


Yes, -128 has one bit set, and will be reported as true. However, I think we are restricting ourselves to REAL powers of two. If we weren't then it would be logical to include -64, -16, etc... (which will be reported as false along with all other negative values)

This topic is closed to new replies.

Advertisement