What is all of these math expression

I challenged myself on codefights. However, whenenver i passed a test, I always find that other use very weird expression to conquer all of the test. Some of that look like " n &=~(1<<(k-1)) " . What the heck is all of these expression and where can I learn that. It is a bit of shame but I do not understand this and often use some very “manual” technique to solve the problem.

All that stuff looks cool for challenge solving, but you are never going to use it in a production setting. I totally agree, and I have no idea what half of those things mean (and neither is practically any other developer looking at your code for the first time). You really shouldn’t use that kind of stuff unless you are in some programming contest, hackathon, etc. Your “manual technique” is the proper way to do it. As to where to learn it… I have absolutely no idea… @PortableStick, @P1xt might have an idea…

I don’t think you are going to use expressions like this a lot in JavaScript. I’ve seen code like this a lot in C and C++, especially embedded work where you are interfacing to the hardware directly. The example you gave specifically:

n &=~(1<<(k-1))

is the same as:

n = n & ~(1<<(k-1))

where:
& is the bitwise AND operator
<< is the bitwise left shift operator
~ is the bitwise negation operator

I believe this expression is clearing bit “k” inside the variable n, where the very first bit (LSB) is labeled bit 1.

1 Like