Live data from Hacker News

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

news.ycombinator.com

61–70 of 82 posts

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

#61
post #50

Earlier quoted context omitted.

There are tons of useful features that aren't supported in C. The basic feature set of C was determined by whatever operations Dennis Ritchie could easily implement on the PDP-11 when he created the earliest C compilers. (Fun fact: the bitwise operators even preceded the Boolean operators like && and ||.) If the question is "why isn't bit indexing supported" then "because processors didn't (and don't) support it" is…

arianvanp had the point of my question correct.

Now to answer your question:

Yes some languages actually support this:

http://www.erlang.org/documentation/doc-5.6/doc/programming_...

http://hackage.haskell.org/packages/archive/binary-strict/0.... (I have no idea why this isn't a Functor nor an Applicative Functor, so this is a bad example. and I know 'language features' in pure functional programming is cheating, butm eh)

http://en.wikipedia.org/wiki/Bit_field -- BE WARNED. C bitfields might give you access to bits, but they're still aligned to the boundary of your system (DWORD boundary on x86 for example)

e.g.:

    struct a {
       unsigned a: 13;
       unsigned b: 12;
    };

    sizeof(struct a) => 4;
in gcc they've got a flag to force structs to their actual bit size:

   struct a {
      ...
   }__attribute__((packed));

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

#62

Anyone know why bitwise AND is so slow in chrome V8 ??? http://jsperf.com/even-or-odd/2

In theory js doesn't have integers. In practice, it is dependent on the available optimizations in the browser. I saw no appreciable difference on firefox, for example.

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

#63
I use them all the time but I do so in embedded environments where memory (and efficiency) is tight, and bitwise operations are always needed to access the hardware registers.

I see little use for it in other applications, unless these are extremely demanding (such as graphics engines).

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

#64
post #10

I use them a lot when I'm designing hardware, but I never use them when I'm doing "regular programming", there's no reason to do it if I can use more readable, higher level constructs.

When writing network protocols or de-marshalling binary data it's common to want to know which bits are set in a byte. You might do a & 4 == 4 to test the 3rd bit is set, how would you do this with 'regular programming'?

I'm sad to say I've seen code where the answer is sprintf hex output into a temp string and some string functions to see if the last single char substring eq and/or neq certain values at the string level correlating with the 3rd bit being set (so ends in hex 2, well that means bit 3 can't be set... ends in hex 4 ding ding ding we have a winner) Only a slight upgrade in readability on a chain of mystifying if/then/else is a case statement with 16 possibilities. Only a slight upgrade on that is "optimizing" for speed by squirting out octal so your lookup stable is half the size of a hex one.

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

#65

I've found that people who list any of the following on their resume are way more likely to know how to bitwise their way out of a paper bag: - C (not "C/C++", those people generally mean "C++") - video codecs - compression of any kind - encryption / cryptography of any kind - embedded anything

>>those people generally mean "C++"

I've never worked on C++(as in on commercial project or in a C++ team) however I've always mentioned it on my resume as C/C++, because I've worked on C(Kernel wrt Android and a little bit more) but for C++ I've only built one academic project(very minor) during UG and also took (2nd yr) DS in C++. I just do not want to miss a good job offer which requires C++. Because in the interview all (mostly) they are concerned about are DS/Algo etc.

So, here I intend to mean "I know C" and can work with "C++" too.

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

#66

I use "&" and "|" when i use enums, that act as options. I.e. var myCarExtras = Extra.Radio | Extra.PowerWindows; Where Extra = { Radio = 1, PowerWindows = 2, Tint = 4, Rims = 8 }; Then I can do: if (Tint | powerWindows) { return totalPrice + 1000; }

C# programmer? I think this is one of the nicer little features in that language.

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

#67
post #52

I guess programming languages don't really encourage using bit wise operations. On top of it, it doesn't have many advantages. It takes much less memory, but takes more lines to write, while on the hardware, it's the opposite: more memory, not so fast processors. What always bugged me, is that there are no real programming facilities to take advantages of bits, like encoding many variable in on 32 bit integer variabl…

Although not part of the language proper, C++'s STL has vector that packs boolean flags into a bitvector (a now regrettable optimisation, since it doesn't function the way a container should - a story for another time). There is also bitset, which is similar but static.

Erlang has a beautiful language feature for this: http://www.erlang.org/doc/programming_examples/bit_syntax.ht... It uses pattern matching to unpack variable length fields in a bitvector. So beautiful.

Edit: Check arianvanp's post above https://news.ycombinator.com/item?id=5506902

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

#68
I believe my introduction to bit shifting is wrapped up somewhere in the quagmire of my first few months of computer science in college. But I actually became pretty comfortable with their usage since the first major piece of software I worked with, vBulletin, made extensive use of bitwise operations in the ACL and options matrices.

It was a super fast, if visually opaque, way of doing the calculations required to facilitate a very complex permission system. I imagine Facebook works in a similar way, if on a somewhat grander scale.

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

#69
Having done a significant amount of assembly language programming in the past, understanding bit-wise operations is required. In my first job out of college, I was parsing bytes of data where each bit represented a multiple choice answer to a question. So, yea I'm tight with bit-wise operations. And, I'm glad that I have this knowledge, because a good deal of Raspberry Pi projects, where I'm parsing data from a chip on the i2c bus, require it.

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

#70
post #10

I use them a lot when I'm designing hardware, but I never use them when I'm doing "regular programming", there's no reason to do it if I can use more readable, higher level constructs.

When writing network protocols or de-marshalling binary data it's common to want to know which bits are set in a byte. You might do a & 4 == 4 to test the 3rd bit is set, how would you do this with 'regular programming'?

    struct header __atribute__((packed)) {
      unsigned int highnibble : 4;
      unsigned int flag_foo : 1;
      unsigned int flag_bar : 1;
      unsigned int flag_baz : 1;
      unsigned int flag_qux : 1;
    }
    
    if (((header)value).flag_bar) {
Post reply on HN