1. 19
andreinc.net
1.

2. 3

I like the introduction. I think it’s worth guarding the puzzle spoiler a bit more, in case any of your readers haven’t encountered it. There are a finite number of things this neat, and everyone deserves the chance to discover them on their own.

Some fairly nitpicky feedback:

• C doesn’t guarantee two’s complement yet, but it probably will quite soon.
• There are no C implementations where CHAR_BIT is 7 (if they defined CHAR_BIT as 7 they wouldn’t be C implementations).
• The satisfying XOR example isn’t very illustrative, since it happens to do the same thing as OR here.
• Zero isn’t really considered positive or negative, imv. The convention is rather the treatment of 1000… as negative, which is mathematically no more or less correct than treating it as positive, but allows the MSB to correspond exactly with negativeness.
• The equivalence of shifts and multiplication isn’t mentioned in their introduction. I think it ought to be.
• The symmetry between AND/OR isn’t mentioned, although hinted at by the reused wording (which was nice). Neither is the fact that they distribute over each other.
1. 2

Thank you for spending time to read the article and provide valuable feedback.

I will add more sections based on the feedback I’ve received. There is some code that’s not pedantically correct that I need to modify, I will also try to incorporate your suggestions.

1. 1

Hello, I’ve looked into the C standard (C99) and I found a few mentions of two’s complement. For example:

The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two’s complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits

Can you point me in the right direction, if you’d like, to understand how it’s not in the standard ? The pdf document is quite “verbose” and I’ve got lost.

2. 2

Good stuff! I’ve actually implemented next-power-of-2 the naive way recently, as part of sizing a hash table; I’ll change it to the clever way :)

I see you referenced Matters Computational at the end — that’s definitely my go-to reference for bit twiddling, and it’s freely downloadable.

1. 1

If you are using GCC there’s also an extension for doing that. On some architectures there are usually instructions for it.

Thanks for the feedback.