

Do you know your bitwise operators? - amenghra
http://quaxio.com/know_your_bits/

======
warmfuzzykitten
Validator seems broken.

~~~
fourspace
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

~~~
dalke
It appears to have a limited JS validator to prevent you from using anything
other than the limited operators it allows.

~~~
sp332
It really doesn't like having anything after "return" except a single value or
variable.

~~~
dalke
That's not it. For example, it accepts "return x|x&x;" . The restriction is
that only a handful of Javascript operations are allowed. Anything else gives
a hard-to-understand error message.

------
conductr
This 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

~~~
wellingtons
To do this exercise in javascript is pretty silly. But as a systems developer,
I can tell you that bit manipulation happens all over the place in my line of
work.

~~~
amenghra
There is nothing inherently javascript about this, besides providing a UI to
validate the answer (that's when the validator works...)

------
Someone
That will get fairly hairy, given that JavaScript doesn't have integers. x
should be expected to be a IEEE 754 double (can one discriminate between NaNs
in JavaScript?), but might also be a string (does "@" have one bit set?, or
might it be EBCDIC?), an array, an object, null or undefined (do those have a
bit pattern?)

~~~
amenghra
JavaScript supports various bit manipulation operators, which treat their
operands as 32 bits (and return a numerical value).

~~~
Someone
Because of that, I am not sure can use them to count the number of 1 bits in a
JavaScript number. For example, if x&0xFFFFFFFF equals 356621, what do you
know of x?

------
norswap
This 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.

~~~
ejk314
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/](http://jsfiddle.net/Ayzkt/2/)

~~~
ejk314
Here's an ugly solution.

[https://gist.github.com/anonymous/8364074](https://gist.github.com/anonymous/8364074)

~~~
amenghra
Nice. Find a shorter one now (afaik, the best score is 12 and it's like in
golf, smaller scores are better).

------
10098
The validator doesn't work at all.

~~~
amenghra
Sorry. Fixed.

