News for hackers. Stuff that matters.
Re:Do you know your bitwise operators? by conductr on Friday January 10, 2014 @02:31PM (#7038789)
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? by joshvm on Friday January 10, 2014 @04:57PM (#7039801)
Embedded applications use bitwise ops all the time. Particularly for performing changes to registers.

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 << BIT)

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 << BIT))

Then you can say:

if( getbit(myregister, 5)){ // do something }

Fun fun :)

Re:Do you know your bitwise operators? by blt on Friday January 10, 2014 @03:06PM (#7039017)
- 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_pale...

[2] http://arduino.cc/en/Reference/PortManipulation

Re:Do you know your bitwise operators? by gamegoblin on Friday January 10, 2014 @02:42PM (#7038851)
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 << 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.
Re:Do you know your bitwise operators? by wellingtons on Friday January 10, 2014 @03:03PM (#7038993)
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.
Re:Do you know your bitwise operators? by amenghra on Friday January 10, 2014 @03:32PM (#7039168)
There is nothing inherently javascript about this, besides providing a UI to validate the answer (that's when the validator works...)
Re:Do you know your bitwise operators? by aguynamedrich on Friday January 10, 2014 @02:45PM (#7038865)
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? by 10098 on Friday January 10, 2014 @03:20PM (#7039101)
This is mostly done in lower level code, so no wonder you have never bumped into it. One easy example, though is passing a set of binary options around, which is useful, for example, when you want to say "enable those features, and disable those other features". You can pack up to 32 options into a 4-byte integer.
Re:Do you know your bitwise operators? by amenghra on Friday January 10, 2014 @03:31PM (#7039165)