Live data from Hacker News

Poll: How many programmers don't know about bit-wise operations?

news.ycombinator.com

31–40 of 82 posts

Re: Poll: How many programmers don't know about bit-wise operations?

#31
post #29

There is no correct option for me. My actual answer is "I know about them and could do complex things with them when they're the right tool, but for the kind of programs I write they're never needed."

Same here, I voted the first one that seems the most closest to it.

Re: Poll: How many programmers don't know about bit-wise operations?

#32
I've forgotten when I learned this stuff. Recently, I've been teaching people who have been programming for years how to reverse engineer. It's surprising how much trouble they have grasping trivial operations like "a & a" or "b ^ b", and understanding arithmetic equivalents, like using "a I do feel that this poll is biased, because people prefer to declare their knowledge than admit their ignorance.

Re: Poll: How many programmers don't know about bit-wise operations?

#33
I used them in 1995/6 writing a DOS game while learning C and once in a C++/MFC interview question in 1998.

Never once used them in actual application development or web development since 1996. For me and the stuff I work on (web dev mostly) they're unreadable by others and any performance gains wouldn't be noticed or even cared about.

Re: Poll: How many programmers don't know about bit-wise operations?

#34

I have always wondered why I was taught binary as if they were arrays, but programming languages never allowed me to use bits like arrays. It's always masking and shifting uint16_t or int32_t, instead of what I really want, "result = bits[3] xor bits2[3]".

You can do this with bit field structs in C: http://www.cs.cf.ac.uk/Dave/C/node13.html

Super handy when you're dealing with oddly packed int representations and such.

Re: Poll: How many programmers don't know about bit-wise operations?

#35
post #13

((width * bytes_per_pixel)+3&~3) anyone? Can't remember how many times I've done that for Windows bitmap stride. WORD/DWORD/QWORD alignment was something I had to do frequently in C or C++ code. Even fixed point math for speed in the really old days.

hehe, also sin/cos tables ;)

Re: Poll: How many programmers don't know about bit-wise operations?

#36

I like the elegance of bitwise operations and use them sometimes in signal processing applications to avoid a performance hit, though I'm no expert. But I avoid them in any high level code because a) the compiler is likely going to optimize that anyway and b) to be as human-readable as possible, the code should clearly describe the objective rather than exploit the underlying mechanism. In other words if I want to mu…

This is where macros (C) come in to play. Using macros (do all the bit shifting and masking inside) you can write very human readable code that is easy to maintain and while at the same time offers all the advantages of using bit-masks.

The major advantage of bitwise operators for me is the memory savings that come with them. I used to write the route calculation algorithms for routers and every bit/byte save had an tremendous impact on the total capacity of the router. We use bitfields to set and store various things from simple flags to multi-bit values, all inside a single integer variable.

Re: Poll: How many programmers don't know about bit-wise operations?

#37
If you're not working with device drivers, embedded systems, or legacy code/filetypes you probably shouldn't be using bitwise operations. It can be confusing to other programmers, make your code a mess, and you're repeating optimisations probably found in libraries.

This is just a generalisation of course, I use them on occasion. Mostly with string encoding oddities.

For me using excessive bit operations is a red flag. I wrote a messy 1000 line file format in C, I found out by chance there was a serialization function in the library. 10 lines, done. I repeated this mistake when I started learning python (pickle, you're the best).

Re: Poll: How many programmers don't know about bit-wise operations?

#38
Bitwise operations are still common and necessary in dealing with matrices and arrays in Matlab/numpy etc.For ordinary code they would be an unnecessary layer of complexity and perhaps even a false source of optimisation.I tend to trust that the language designers know what they are doing more than I do by default at that level of abstraction.

Re: Poll: How many programmers don't know about bit-wise operations?

#39
post #11
post #5

Earlier quoted context omitted.

At the moment, the linked submission is sitting at 2 points. The default is 1 point, and I'm the +1 up-voter. People writing code should know these things, but the move away from learning real low level languages means many people never learn these things. NOTE: The "low level" languages are assembly, microcode, and machine code, but never C. In spite of claims to the contrary, C is a high level language. When employ…

Because 90% of all programmers don't do much than writing plain CRUD applications.

I see claims like this regularly, but never any data to support it. Do you have anything to back it up?

Re: Poll: How many programmers don't know about bit-wise operations?

#40
post #27

I have always wondered why I was taught binary as if they were arrays, but programming languages never allowed me to use bits like arrays. It's always masking and shifting uint16_t or int32_t, instead of what I really want, "result = bits[3] xor bits2[3]".

The answer is hardware design. Most machines are at best "Byte Addressable" which means you have an address for a specific byte. In some cases, even if the system is "Byte Addressable" it's still actually faster to use blocks of bytes (2Byte/16bt, 4Byte/32bit, 8Byte/64bit). The reason why this is true is because the hardware design was optimized to handle blocks. Though I'm sure "Bit Addressable" hardware exists, I'v…

yes, but we don't care. A language is agnostic of the underlying system. Even the C standard is built on top of a machine that doesn't really exist.

Anyhow, the point is, limitations of the underlying system aren't excuses for why this syntax isn't implemented. If I can write bitmasks, then so can a compiler, right?

Post reply on HN