Earlier quoted context omitted.
I keep getting various parse errors like Expected assignment, comment or return statement but " " found. When trying to get rid of parse errors, I did something like "return x" and it says Solution did not return correct output for x=1. Expected output: 1, got: 0 Which is clearly not right. Just tried doing "return 1" and still got Solution did not return correct output for x=1. Expected output: 1, got: 0
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…
Do you know your bitwise operators?
21–30 of 32 posts
Re: Do you know your bitwise operators?
#22The best I could come up with is:
function one_bit(x) { return 1 >>> (x & (x + ~0)) }
("x + ~0" is equivalent to "x - 1")
But this doesn't work when x is 0 All other case should be fine, afaik.
Re: Do you know your bitwise operators?
#23This is stated to be a puzzle. But, when does this actually get used? Are there use cases of bit manipulation in JS? I've never understood when using bit operators could be necessary or even useful. Of course, I'm speaking from my experience building web apps; ruby, python, js, php, etc. I don't have much other programming experience
Example, a microcontroller has an 8 bit status register. Each bit controls some hardware function. You wish to change bit 5, but you can't change anything else because it would cause unwanted side effects. So this rules out simply saying myregister = 0b00001000.
Common solution is to make a macro like:
#define setbit(BYTE,BIT) BYTE |= (1 Thus you can say I want to set bit 5 in my register by calling setbit(foo, 5) while keeping the rest of the byte intact. There are analogous macros for clearing and toggling bits.
Or you might want to perform an operation based on a particular bit in a byte, e.g. byte 5 is a status flag that tells you when a timer has overflowed.
#define getbit(BYTE, BIT) !!(BYTE & ~(1 Then you can say:
if( getbit(myregister, 5)){ // do something }
Fun fun :)
Re: Do you know your bitwise operators?
#24This is making me crazy, does someone have the solution? The best I could come up with is: function one_bit(x) { return 1 >>> (x & (x + ~0)) } ("x + ~0" is equivalent to "x - 1") But this doesn't work when x is 0 All other case should be fine, afaik.
Re: Do you know your bitwise operators?
#25This is making me crazy, does someone have the solution? The best I could come up with is: function one_bit(x) { return 1 >>> (x & (x + ~0)) } ("x + ~0" is equivalent to "x - 1") But this doesn't work when x is 0 All other case should be fine, afaik.
That would work if the `>>>` operator worked as expected, but actually it just looks at the first 5 bits of the second variable so that `a>>>b` is effectively equivalent to `a>>>(b&31)`. His description of it is wrong. http://jsfiddle.net/Ayzkt/2/
Re: Do you know your bitwise operators?
#26Validator seems broken.
Re: Do you know your bitwise operators?
#27The validator doesn't work at all.
Re: Do you know your bitwise operators?
#28Re: Do you know your bitwise operators?
#29Validator seems broken.
I certainly can't get it to work. For the first problem, I tried: function one_bit(x) { return x & (x-1) == 0 } Only to see this error: Expected "}" or comment but "&" found. WAT
function one_bit(x) { return x & (x-1) == 0 ? 1 : 0; }
Re: Do you know your bitwise operators?
#30Earlier quoted context omitted.
That would work if the `>>>` operator worked as expected, but actually it just looks at the first 5 bits of the second variable so that `a>>>b` is effectively equivalent to `a>>>(b&31)`. His description of it is wrong. http://jsfiddle.net/Ayzkt/2/
Here's an ugly solution. https://gist.github.com/anonymous/8364074