Live data from Hacker News

Demystifying bitwise operations, a gentle C tutorial

andreinc.net

21–30 of 95 posts

Re: Demystifying bitwise operations, a gentle C tutorial

#21

This long article is a great example of why I believe GPT has a strong future. The article spends several pages to explain hexadecimal, bases, etc. Probably some fraction of the audience already knows that. In that case, the article should automatically adapt and skip that section. Some people will react negatively to the mathematical formulation, preferring the intuitive section that follows instead. But this is a s…

The problem with GPT is there’s a good chance that it introduces information which is plain wrong while doing that transformation.

Re: Demystifying bitwise operations, a gentle C tutorial

#22
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 only have a little grey in my beard so far, but like all optimizations it heavily depends on context. My broad rule of thumb is that if you only care what the code does, you should let the compiler figure it out. If you care how the compiler accomplishes that goal, you should specify that rather than hoping things don't silently break in the future. This is a fairly common thing in crypto and systems code.

But yes, fast inverse sqrt is obsolete.

Re: Demystifying bitwise operations, a gentle C tutorial

#23
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 world is built on it

if not, when will it become?

Re: Demystifying bitwise operations, a gentle C tutorial

#24

This long article is a great example of why I believe GPT has a strong future. The article spends several pages to explain hexadecimal, bases, etc. Probably some fraction of the audience already knows that. In that case, the article should automatically adapt and skip that section. Some people will react negatively to the mathematical formulation, preferring the intuitive section that follows instead. But this is a s…

"Hey chatGPT, paraphrase my comment without the passive aggressive dismissal of its contents."

In all seriousness though, this blogpost does exactly what it intends to do. Demistify bitwise operations and you can't skip the math.

Re: Demystifying bitwise operations, a gentle C tutorial

#25

This long article is a great example of why I believe GPT has a strong future. The article spends several pages to explain hexadecimal, bases, etc. Probably some fraction of the audience already knows that. In that case, the article should automatically adapt and skip that section. Some people will react negatively to the mathematical formulation, preferring the intuitive section that follows instead. But this is a s…

Sure, let's just skip on the minor details that modern computation rests on. Who needed that anyway.

Although I do agree that repeating the basics on every post is cumbersome, it seems adequate for this post.

Re: Demystifying bitwise operations, a gentle C tutorial

#26
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 ?

Oh definitely. Some of this goes back to my 6502 assembly days when there was no hardware multiply instruction. So to multiply by 40, for example. I would shift right 3 bits, store the result, shift right 2 more bits and add the stored result.

Similarly, a fast divisibility test (we’ll assume we’re dividing n by some odd prime p):

1. Shift the bits of p right so that there is a 1 in the last position.

2. If n = p then pn, if n p then pn, otherwise continue.

3. Subtract p and go back to step 1.

(One of my ADD habits during meetings is to find the prime factors phone numbers or anything else very long. I do something similar with the numbers in decimal, but for this, I’ll subtract multiples of the potential divisor to get 0s at the end of the number in decimal. I remember co-workers puzzling over a piece of paper with my notes I left behind in a conference room trying to figure out what the numbers represented and how the process worked.)

Re: Demystifying bitwise operations, a gentle C tutorial

#27
Heads up: the gray_code function does not return the results shown in the table of correspondence above it.

I suspect that this is because both the table and the code used were sourced from Wikipedia, and they correspond to different Gray codes. The table is for the BRGC, but the implementation isn't.

Re: Demystifying bitwise operations, a gentle C tutorial

#28

This long article is a great example of why I believe GPT has a strong future. The article spends several pages to explain hexadecimal, bases, etc. Probably some fraction of the audience already knows that. In that case, the article should automatically adapt and skip that section. Some people will react negatively to the mathematical formulation, preferring the intuitive section that follows instead. But this is a s…

GPT isn't needed. Just hyperlinks, like exist at the top of the document. Paired with the ability to expand sections either in place or in an adjacent view. State the prerequisite knowledge, then give a way to skip past the explanations or have the explanations hidden by default. Permit further expansions up to the limit of what the author cares to provide, can be added to later.

Re: Demystifying bitwise operations, a gentle C tutorial

#29
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 ?

Depends on the situation. The compiler is smart, but in a way it's also dumb. It's very good at recognizing certain patterns and optimizing them, but not all patterns are recognized, and thus not all optimizations are applied, let alone consistently applied.

For example, see the article and discussion on Bitwise Division from two days ago: https://news.ycombinator.com/item?id=34981027

Re: Demystifying bitwise operations, a gentle C tutorial

#30
post #17
post #8

I thought this was going to be ridiculous but it's actually a really good. It's clear enough that you could extend it down to showing how logic gates work. The only thing missing is a little endian discussion; it assumes big endian (network byte order), and that may be confusing for all those x86 users out there.

> 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).
Post reply on HN