Live data from Hacker News

Do you know your bitwise operators?

quaxio.com

1–10 of 32 posts

Re: Do you know your bitwise operators?

#3

Validator seems broken.

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

Re: Do you know your bitwise operators?

#4
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

Re: Do you know your bitwise operators?

#5
post #4

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

The 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 
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.

Re: Do you know your bitwise operators?

#7
post #4

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

The 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.

Re: Do you know your bitwise operators?

#8

Validator 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

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

Re: Do you know your bitwise operators?

#9
post #8

Earlier quoted context omitted.

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

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

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

Re: Do you know your bitwise operators?

#10

Validator seems broken.

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

Post reply on HN