Live data from Hacker News

Understanding the power of bitwise operators

deusinmachina.net

11–20 of 48 posts

Re: Understanding the power of bitwise operators

#11
OT but his mention of the calculator reminded me of discovering just yesterday that the built in Mac calculator has a Programmer mode (apparently Windows too) for hex, etc.

I’d been using the RPN/scientific mode for ever but never new about Programmer mode.

I’m actually doing a fair amount of hex conversions and bit fiddling these days (webassembly bytecode) so it was actually something useful.

Re: Understanding the power of bitwise operators

#15
One of my earliest exposures to the power of bitwise operators happened when I was learning to write chess engines. This was right around the time 64 bit processors were hitting the markets, which allowed storing one bit of information per square, making it ideal for efficient processing via bitwise operators. There were some clever operations that allowed finding sliding piece collisions in only a few assembly instructions (with prodigious use of BSR and BSF) and a small amount of precomputed memory.

For anyone interested in binary chess math:

https://www.chessprogramming.org/Bitboards#General_Bitboard_...

Re: Understanding the power of bitwise operators

#16
post #12
post #6

Is it an autocorrect issue or the writer only-heard-never-read that the "^" character is called a “carrot”?

The "^" character is correctly named the "caret".

As someone who is used to the French pronunciation of “caret”, “carrot” is hilarious.

Also, in INTERCAL the character is called “shark” or “sharkfin”.

Re: Understanding the power of bitwise operators

#18
post #8

Who is this article for exactly? It starts off by trying to relate to the reader by presenting the point about learning a new programming language, but then goes on to explain one of the most fundamental concepts of computing, as if the reader is a complete novice. I would imagine that nearly every person with programming experience, whether that be formal or not, would have at least some familiarity with binary repr…

> would have at least some familiarity with binary representation and bitwise operations.

There's a whole generation of programmers that aren't interested in, and don't need, "low level" bitwise operations. I would claim that's a "good thing", since it means they're letting someone else do the "boring stuff" by using libraries, allowing them to spend more time solving higher level problems.

When I was going to school, some sort of assembler was the first language you learned. These days, it's usually Python or Java. With a "not real programmers" silliness aside, I hope the tedium continues to decline, and is left for those interested in it, increasing productivity for everyone.

Re: Understanding the power of bitwise operators

#19

Really weird to start with 'that one section that most of us skip' and then use an _instruction set emulator_ as the real world example. Who exactly is both frightened of the & symbol and wants to learn about instruction encoding?

I was hoping for something really unusual after the introduction

"While at first, they might seem obscure, unhelpful, or tools for people who write in low level programming languages, they do serve a purpose. "

But then... Instructions set, assembly language...

Re: Understanding the power of bitwise operators

#20
I was just doing some bit shifting yesterday. I wanted to pass a drawing function a Uint16 that would represent a 4x4 black and white pixel pattern. It was a little hack to create early MacOS-like pixel patterns in SDL. SDL has primitives for line and rectangle drawing with solid colors but little else.

So I wrote some straight C, some nested for loops for row/column of the area to be patterned. I used row % 4, column % 4 to map to the appropriate bit within the Uint16 "pattern". Some bit shifting, masking and I knew whether to fill the pixel with foreground or background colors.

I always enjoy bitwise operations. (There's I guess the classic programmer problem of swapping the values of two numbers in two registers in place.)

Post reply on HN