That XOR Trick

There are a whole bunch of popular interview questions that can be solved in one of two ways: Either using common data structures and algorithms in a sensible manner, or by using some properties of...

The mother lode of #BitTwiddling tips (apart from the #book “Hacker’s Delight”, by Henry S. Warren Jr, and “HAKMEM” by the MIT hackers):

“Bit Twiddling Hacks”, Sean Eron Anderson (https://graphics.stanford.edu/~seander/bithacks.html).

On HN: https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=Bit%20Twiddling%20Hacks&sort=byPopularity&type=story

#Math #Optimization #Compilers

Bit Twiddling Hacks

A leap year check in three instructions

How to test for leap years (until year 102499) in the proleptic Gregorian calendar with just three 32-bit instructions, with detailed explanation of the bit-level tricks.

I'm sure this has been done many times before, but I'm still happy to be bit-twiddling in #shaders. The star is this line:

(vertex_index & 3u) + (vertex_index >> 2u)

"Give me the first two bits but add one if the third bit is set.”

The shader generates a UV-mapped quad (right-triangle pair) from the 0 through 5 vertex indices. The screenshot code is abridged; full code is at the link.

https://github.com/shanecelis/bevy/blob/add-viewport-post-processing/crates/bevy_core_pipeline/src/viewport_vertex_shader/viewport.wgsl #gamedev #bevyengine #bittwiddling #wgsl

In 2013, past me wrote some mysterious code. Effectively, it was this.

uint8_t decay(uint8_t x) { return (x >> 1) | (x >> 2); }

Sort of a low level multiply by 0.75, but with an | instead of a +. The target was an 8 bit AVR. Is there any good reason for using | here? I vaguely remember a bug where certain values didn't decay, but testing shows all 255 nonzero values of x do decay.

Any thoughts?

#CProgramming #BitTwiddling

Re: coding.

If it ain't native ASM, it ain't shit.

POP!

#BitTwiddling

**scampers away cackling maniacally**

I made a tiny #Swift package for decoding Base32 values.

https://gitlab.com/mflint/base32

#iOSDev #BitTwiddling

Matthew Flint / Base32 · GitLab

GitLab.com

GitLab

Nice – #RaymondChen is always a pleasure to read:

“On Finding The Average Of Two Unsigned Integers Without Overflow”, Raymond Chen (https://devblogs.microsoft.com/oldnewthing/20220207-00/?p=106223).

Via HN: https://news.ycombinator.com/item?id=30252263

#Arithmetic #Integers #Average #Mean #Overflow #BitTwiddling #Efficiency #Assembly

On finding the average of two unsigned integers without overflow - The Old New Thing

Another code golfing adventure.

The Old New Thing

I’d like to think that Tarah’s colleague here was a master troll 🤡:

“A Bit Overcomplicated”, The Daily WTF (https://thedailywtf.com/articles/a-bit-overcomplicated).

Via HN: https://news.ycombinator.com/item?id=28072477

#Rust #Bit #Binary #Shift #BitTwiddling #Programming #WTF #Complexity

A Bit Overcomplicated

Lets say you have a 64-bit integer. You want the first 42-bits. Now, if your language has a bitshift operator, you'd be tempted to do something like largeNumber >> 22. But what if your language also has all sorts of advanced stream processing and map functions? It'd be a shame to not use those. Tarah's shop uses Rust, and thus they do have all those exciting advanced stream processing. Her co-worker decided they just needed to be used:

I'm working on a pure Ruby network packet encode/decode module using Array#pack and String#unpack. I did one of these about 16 years ago where everything was in one class. This time each format is a separate class like Uint8, Uint16, Uint32 and TimeStamp (which is a composite of 7 bytes) - with class methods encode and decode.

I'm kinda liking this current approach. Anyone do something similar? I'm expecting a little performance hit but frankly don't care at this point. #bittwiddling