News for hackers. Stuff that matters.
Re:A Bit Overcomplicated by BulgarianIdiot on Thursday August 05, 2021 @09:40AM (#28073587)
Bitwise shifting isn't that important, unless you happen to work with binary formats and protocols, which should be distinct low-level layer in any app, and ideally relegated to a library that does it better.

Back in time it was known for being a fast way to multiply and divide by powers of 2. This hasn't been true for a long time now, and in the rare cases it is, most optimizing compilers will bitwise shift where in your source code you divide or multiply.

Re:A Bit Overcomplicated by jcranmer on Thursday August 05, 2021 @02:44PM (#28077821)
> Back in time it was known for being a fast way to multiply and divide by powers of 2. This hasn't been true for a long time now, and in the rare cases it is, most optimizing compilers will bitwise shift where in your source code you divide or multiply.

Shifting is still faster than multiplication--a shift is a 1-cycle operation, multiplication typically 3 cycles. Division is somewhere in the region of a dozen cycles or more. (Also, division is one of the assembly operations whose time is data-dependent). So it's faster to replace a multiplication with a single shift (but not multiple shifts), and to replace division with shifts or even multiplies--there's a fast division replacement that replaces a division by a small constant with a multiply-hi and shift.

Will compilers replace multiplies and divides with shifts? Kind of. If you multiply a value with a constant (after inlining/constant propagation/etc.) that's a power of two, or if you multiply it with an expression 1 << var, then that will be converted to a shift.

For division, it's even more complicated because shifts and division by power of 2 are not equivalent if the type is signed: -1 / 2 is 0 but -1 >> 1 is -1, so the transform is not legal unless the compiler can find out either a) the value is never negative or b) the division is exact.

Re:A Bit Overcomplicated by NwtnsMthd on Thursday August 05, 2021 @05:14PM (#28079947)
With a DSP core it often gets even better, the bit shift might be free. Say you have the instruction ACC = ACC + P << PM, that could compile to a single instruction, if you're not writing assembly directly.

Additionally if you're ever working directly with hardware, writing drivers, or firmware, bit-shift operations are often the best (and most common) way to get stuff done.

Re:A Bit Overcomplicated by BulgarianIdiot on Thursday August 05, 2021 @07:14PM (#28081294)
I wouldn't call it better because CPUs have a similar "sum and multiply" instruction, and GPUs are pretty much made of such units.

The problem with bitshifting is that it's just a special case of multiplication and division. So if you want your hardware utilized, you don't scale bitshifts, that'd be largely useless. You scale multiplication and division, and this is what modern hardware does.

Bitshifting is very simple in terms of hardware, but scale beats simplicity.

Re:A Bit Overcomplicated by rasz on Thursday August 05, 2021 @06:44PM (#28081032)
Re:A Bit Overcomplicated by Alupis on Thursday August 05, 2021 @04:38PM (#28079407)
> unless you happen to work with binary formats and protocols

ie, embedded systems and microcontrollers - which is where binary formats and bitwise operations still rule out of necessity (limited processing power, precious cycle counts per operation, limited memory and storage, etc).

I highly doubt your average web or app developer will ever need to use bitwise operations. They're harder to read and understand quickly, and none of the limitations that make bitwise operations attractive exist on devices with gigabytes of ram and terabytes of storage.

Re:A Bit Overcomplicated by FpUser on Thursday August 05, 2021 @05:17PM (#28079983)
>"They're harder to read and understand quickly"

Disagree. Never had problem with bits. Also it is one of the basics of general computing so it would not hurt to know about the subject.

Re:A Bit Overcomplicated by Alupis on Thursday August 05, 2021 @05:30PM (#28080164)
Doesn't hurt to know it, sure, but it's entirely not useful for a large majority of developers these days.

Something like:

    (some_var & 0x003ff000) >> 12;
Or even:

    _displayControl &= ~DISPLAY_ON;
Is not immediately clear what is going on (other than some masking and shifting is happening), and these are simple cases without a lot going on. To understand these operations, we have to figure out what's significant about these numbers, what the variables might be here, etc.

It's a lot easier to understand:

    display.disable();
Which is what most higher-level-language developers (web, apps, even backend, and more) will use and expect to find in a codebase.
Re:A Bit Overcomplicated by BulgarianIdiot on Thursday August 05, 2021 @07:18PM (#28081327)
What you're saying: "In practice good modern API design prefers explicit intent over low-level details like exposing bitshifting for flags"

What people are saying to you: "You say bitshifting is complex, so if I say bitshifting is simple, it means I'm smarter than you"

Re:A Bit Overcomplicated by FpUser on Thursday August 05, 2021 @07:30PM (#28081414)
>"so if I say bitshifting is simple, it means I'm smarter than you"

To BulgarianIdiot: This is quite a conclusion.

Re:A Bit Overcomplicated by FpUser on Thursday August 05, 2021 @06:00PM (#28080546)
>"Doesn't hurt to know it, sure, but it's entirely not useful for a large majority of developers these days"

I read about a lot of things that are not immediately useful for me. Still somehow it falls into nooks and crannies of my brain and helps me to solve various problem when / if need arises. I do not even remember the exact things but general understanding of what is available and how does it functions approximately. If I need details Google does the rest.

>"It's a lot easier to understand:

    display.disable();"
No arguments about this one. Even though I do work with bits sometimes I wrap those into something digestible like your example. Inlining helps not to use performance when it is needed.
Re:A Bit Overcomplicated by spaetzleesser on Thursday August 05, 2021 @09:43AM (#28073621)
I think it’s important though to know that these operators exist.
Re:A Bit Overcomplicated by AnIdiotOnTheNet on Thursday August 05, 2021 @11:00AM (#28074656)
More than that, I think it is important to have a certain level of understanding about how computers actually work if you're going to be programming them as a career, and boolean logic and bitwise operators are a very basic part of that.
Re:A Bit Overcomplicated by BulgarianIdiot on Thursday August 05, 2021 @03:22PM (#28078287)
How computers work is a very stretchy term. And what you need to know depends what you're working on. For example if you're scripting you don't need to know a thing about how computers work, your concerns are mostly at the software level.

If you're writing a high-performance server or a game engine, you need to understand the topology of computers. Network performance and protocols, disk, memory and cache architecture. But you still don't need to understand every machine code on the CPU.

We're risking falling into the trap of "more is more" like a chemistry teacher that is adamant that you need to absolutely understand covalent, ionic and so on bonds, because everything around you chemistry, including you. I mean how would you find a job as an accountant and start a family without understanding chemistry?

Re:A Bit Overcomplicated by godshatter on Thursday August 05, 2021 @04:29PM (#28079306)
I take your point about the risk of falling into the trap of "more is more", but we're not talking about how to build NAND gates - we're talking about how integers are represented by bits and how that knowledge can be leveraged. For instance, if the author of the code showcased in the article had a bit more knowledge about how integers are stored internally, they would have realized they could have simply divided by 2^22 even if they didn't have access to a low-level bit-shift operator.
Re:A Bit Overcomplicated by BulgarianIdiot on Thursday August 05, 2021 @05:05PM (#28079829)
The solution in the article is a bit of a mystery. To come up with that particular solution you already know what bits are, because the first step is formatting the number as bits. Everyone, even people clueless of computers, know that computers are "ones and zeroes" i.e. binary.

Keep in mind "The Daily WTF" is widely suspected to be making up most of its articles.

Re:A Bit Overcomplicated by majewsky on Thursday August 05, 2021 @10:27AM (#28074174)
Which OP obviously does.