Live data from Hacker News

Demystifying bitwise operations, a gentle C tutorial

andreinc.net

51–60 of 95 posts

Re: Demystifying bitwise operations, a gentle C tutorial

#51
post #32

Funnily enough just spent a day tracking down a weird problem in an embedded system - some event timestamps were getting corrupted in a weird way. The pattern wasn't obvious until in desperation I dumped them in hex and found the second MSB was toggling between 0 and 1 (whereas it should have been been part of a count sequence). That told me exactly where to look - where the count was re-assembled from four bytes and…

This is a good example

Why C's implicit conversions are trash. Why big endian is trash. Why you should do a reverse copy instead of stuff like this.

Re: Demystifying bitwise operations, a gentle C tutorial

#52

Earlier quoted context omitted.

>Fundamental knowledge yes? no? hard to say The number of times I've needed this knowledge during all years of formal education and then years of work would be probably around 3. Then I started working with C and close to hardware and it became something that I need everyday. It's feels like bit proficiency is only useful in some very specific domains. Thought: is HTTP foundational knowledge nowadays? after all whole…

I think if you work with UUIDs, it's worth learning about bitwise stuff. So backend developers in general. It's not a day-to-day skill, but it is one I've used at just about every job in the last decade to elegantly and efficiently solve hard problems regarding idempotency and randomness.

When would you want to perform bitwise operations on a UUID?

Re: Demystifying bitwise operations, a gentle C tutorial

#53
post #41

Earlier quoted context omitted.

tx_data[44] = utime>16; Good catch! I wonder if this would have been flagged with -Wall?

Nope, code was clean with -Wall on arm-9 compiler (i.e. gcc). Interesting thought now you say that (no warning) - only found it because of seeing the pattern (apropos the article) and from that having a good idea what was causing it (knowing the int was assembled a byte at a time). If I had a criticism of modern compilers, it's the blizzard of uninteresting warnings ("strncmp takes const char star, did you really mea…

All warnings are uninteresting until they're not.

> ("strncmp takes const char star, did you really mean to pass it unsigned char star")

This warning (with a different function) actually saved my bacon once, pointing me to a very obscure bug in the code.

My practice is to use -Wall and make sure that the code compiles without any warnings at all. Then I don't have a deluge of warnings to wade through.

Re: Demystifying bitwise operations, a gentle C tutorial

#54

Earlier quoted context omitted.

>Fundamental knowledge yes? no? hard to say The number of times I've needed this knowledge during all years of formal education and then years of work would be probably around 3. Then I started working with C and close to hardware and it became something that I need everyday. It's feels like bit proficiency is only useful in some very specific domains. Thought: is HTTP foundational knowledge nowadays? after all whole…

I think if you work with UUIDs, it's worth learning about bitwise stuff. So backend developers in general. It's not a day-to-day skill, but it is one I've used at just about every job in the last decade to elegantly and efficiently solve hard problems regarding idempotency and randomness.

Or if you work directly with hardware, as I do. I use bitwise stuff constantly. Or if you're doing certain obscure kinds of highly optimized mathematical operations.

Re: Demystifying bitwise operations, a gentle C tutorial

#55
post #18

I’ve found significant code in c/c++ where bitwise operations are done for things like division etc by shifting a certain way. I Can imagine in the past, this was “faster”, yet clang/gcc can emit the same by just writing a basic A/B function. Seems the win goes to readability by reducing some of these old school hacks. What say you, greybeards ?

> I’ve found significant code in c/c++ where bitwise operations are done for things like division etc by shifting a certain way.

Oh, yes. I used to do that sort of thing frequently because the time savings was significant enough. As you say, though, compilers have improved a great deal since then, so it's not generally needed anymore.

If stupid bit tricks like that aren't necessary, they shouldn't be used. They do bring a readability/mental load cost with them.

Re: Demystifying bitwise operations, a gentle C tutorial

#56
post #34
post #18

I’ve found significant code in c/c++ where bitwise operations are done for things like division etc by shifting a certain way. I Can imagine in the past, this was “faster”, yet clang/gcc can emit the same by just writing a basic A/B function. Seems the win goes to readability by reducing some of these old school hacks. What say you, greybeards ?

Nowaways you just write 'divout = divin/8192' and assume the compiler is going to do the right thing (and very possibly do something deeper than "divin>>12" at the assembler level). Makes me wonder who pays attention to this sort of thing these days :)

I do! When optimizing code that must run obscenely fast, I look at the assembly the compiler spits out to make sure that it can't be improved on, speed-wise.

Usually, it can't -- but sometimes...

Re: Demystifying bitwise operations, a gentle C tutorial

#58
post #30
post #17

Earlier quoted context omitted.

> that may be confusing for all those x86 users out there. It’s pretty much every user these days - even most MIPS based network oriented devices run LE. BE lost many years ago.

The Network is big-endian (network byte order).

That's just a data serialization format, there are zillions of those. The important thing is the endianness of the CPU.

Re: Demystifying bitwise operations, a gentle C tutorial

#59
post #3

While it's based on C, a large chunk of the content is simply a great introduction to number representations in other bases such as binary and hexadecimal etc. Fundamental knowledge and very well explained, and a few insights I've not seen before.

>Fundamental knowledge yes? no? hard to say The number of times I've needed this knowledge during all years of formal education and then years of work would be probably around 3. Then I started working with C and close to hardware and it became something that I need everyday. It's feels like bit proficiency is only useful in some very specific domains. Thought: is HTTP foundational knowledge nowadays? after all whole…

I think it's fundamental because understanding it goes hand-in-hand with understanding how computers actually work. When I interview applicants, I usually ask one or two questions about bitwise operations for this reason -- an engineer who knows how machines work at a very low level is valuable even if their work is not low level.

Re: Demystifying bitwise operations, a gentle C tutorial

#60
post #33
post #6

I suppose the online list of hacks at the 1st reference in the OP [1] makes more sense, but I've got a fondness for the book "Hacker's Delight", which I've owned and browsed for many years now [2]. And checking on that, I see there was a 2nd edition issued 10 years ago. Hmm, might need to pick that up. [1] https://graphics.stanford.edu/~seander/bithacks.html [2] https://en.wikipedia.org/wiki/Hacker's_Delight

There are quite a lot of bit hacks on the web, but Hackers Delight is where it took off for me.It was a massive eye-opener, what was possible and even better doing it the old-fashioned way. The second book is very, very heavy on division and as such it's not really as much 'fun' as the original, however I'd still recommend it! I shared the original Hackers delight with a work colleague, who had a mathematical bent an…

The last time I reached for Hacker's Delight for "productive" use it was specifically for this chapter (I was aware of space-filling curves from some years in the games industry but had never implemented one), and I remember being disappointed at the lack of depth and options compared to the rest of the book.
Post reply on HN