6 ms·
Do you know your bitwise operators?
- warmfuzzykitten 13y agoValidator seems broken.
- gamegoblin 13y agoI 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
- dalke 13y agoI 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))'
- fourspace 13y agoI 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 13y agoIt appears to have a limited JS validator to prevent you from using anything other than the limited operators it allows.
- sp332 13y agoIt really doesn't like having anything after "return" except a single value or variable.
- dalke 13y agoThat'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.
- wellingtons 13y agoYou can't use '-'.
- warmfuzzykitten 13y agoThat returns true or false, which I believe strictly speaking aren't numeric in Javascript. By definition of problem, probably should be: function one_bit(x) { return x & (x-1) == 0 ? 1 : 0; }
- sachleen 13y agoDefinitely. function one_bit(x) { return 1 } Solution did not return correct output for x=1. Expected output: 1, got: 0 umm... ok
- amenghra 13y ago:( Oops, i'll fix it.
- amenghra 13y agoShould be fixed now...
- conductr 13y agoThis 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
- gamegoblin 13y agoThe only time I find myself using bit manipulations in production code is when I am writing a library that uses an int to represent a set of option flags (where each bit is a boolean flag). It's pretty common in C libs to define some constants like int FLAG_1 = 1; int FLAG_2 = 2; int FLAG_3 = 4; //These would actually probably be formatted like (1 << N) ... And then pass in your options to a function like f(... FLAG_1+FLAG_2) so you can have an optional number of flags in a single argument.
- aguynamedrich 13y agoThe first time I used bit manipulation in web apps was for declaring user roles and permissions. A single integer field in your database can be used to declare any combination of up to 32 different capabilities/permissions for users by setting the individual bits in the value. Also, when dealing with colors as an integer value, you can isolate your ARGB values using bit shifting.
- wellingtons 13y agoTo 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 13y agoThere is nothing inherently javascript about this, besides providing a UI to validate the answer (that's when the validator works...)
- blt 13y ago- Packing lots of small pieces of information into a single memory word - for example, 3-3-2 bit RGB [1] - Interacting with hardware that uses certain bits in a register to control behavior - for example, Arduino digital ports [2] - Doing arithmetic extra-fast with numbers that are powers of two - modulo becomes bitwise-and, division and multiplication become shifts [1] http://en.wikipedia.org/wiki/List_of_monochrome_and_RGB_palettes#3-3-2_bit_RGB http://en.wikipedia.org/wiki/List_of_monochrome_and_RGB_pale... [2] http://arduino.cc/en/Reference/PortManipulation http://arduino.cc/en/Reference/PortManipulation
- 10098 13y agoThe validator doesn't work at all.
- amenghra 13y agoSorry. Fixed.
- norswap 13y agoThis 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 13y agoThat 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/
- Someone 13y agoThat 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 13y agoJavaScript supports various bit manipulation operators, which treat their operands as 32 bits (and return a numerical value).
- Someone 13y agoBecause 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?