2 ms·
I get the same thing as you, and I get the same thing when I have "return 0." The puzzle is annoying. For what it's worth, here's my answer: 1>>(x&~(~x+1))&~(1
by dalke 13y ago
I get the same thing as you, and I get the same thing when I have "return 0." The puzzle is annoying.
For what it's worth, here's my answer: 1>>(x&~(~x+1))&~(1>>x) . (I didn't figure out that the tester was broken before I developed my answer.)
I tested it with positive integers on Python. I haven't tried it with negatives in Javascript.
The '(x&~(~x+1))' is a work-around to get 'x&(x-1)', which is 0 if x is a power of two. The (1>> value) is a workaround to get "value == 0". Thus, '1>>(x&~(~x+1))' means 'is x a power of two?'
The ~(1>>x) means 'is x == 0'. Combine the two together gives 1 if a single bit is set, else 0.
- letney 13y agoThe work-around for 'x&(x-1)' I found is one-character longer: '(x&(~x+x+x))'