Live data from Hacker News

Low Level Bit Hacks You Must Know

catonmat.net

11–20 of 49 posts

Re: Low Level Bit Hacks You Must Know

#11

Here's a much more comprehensive collection of bit hacks: http://graphics.stanford.edu/~seander/bithacks.html

It depresses me that something as simple as

    int mask = v >> sizeof(int) * CHAR_BIT - 1;
    unsigned int r = (v ^ mask) - mask;
can be granted a patent (http://graphics.stanford.edu/~seander/bithacks.html#IntegerA...).

Re: Low Level Bit Hacks You Must Know

#13
post #10

Is working on embedded systems really this fun? I might have to change careers...

It was 10-20 years ago. The funnest project I ever worked on was to implement a 17-function motor controller with 40-bit precision onto an MCU with 2K of RAM and 64 BYTES of RAM. That was in '93.

Today embedded systems generally run Linux. You get to write a bit of assembly language code in your bootloader, and then it's just bog standard Unix programming. It's even likely that you'll be doing most of your coding in a scripting language, although it's more likely to be Lua than Ruby...

Re: Low Level Bit Hacks You Must Know

#15
As much as I love bit manipulation, I have to say that most (not all) of them don't provide any real performance gain for C/C++ code (only some geeky satisfaction of writing difficult to interpret code!).. e.g. with any decent compiler...

* if ((x & 1) == 0) performs same as, if ((x % 2) == 0)

* if (x & (1so on.. on the other hand, i find bit operations really handy when the variables in question are to be treated as separate bits than normal base-10..

Re: Low Level Bit Hacks You Must Know

#16
post #2

Bit Hack #6. Turn off the rightmost 1-bit. Now it finally gets more interesting!!! Bit hacks #1 - #5 were kind of boring to be honest. Does anybody know a practical use case for that? I have personally never encountered a situation were I needed to manipulate the right most 1-bit. Otherwise it's a nice introduction to bit hacking.

Let's say you use a bitmask for keeping track of free registers in a compiler. To "allocate" a register, you'll want to clear a bit, to indicate that the register is no longer free.

That's the context I learned this trick in. It's used in the Embarcadero (ex-Borland) compilers (Delphi, C++, etc.).

Re: Low Level Bit Hacks You Must Know

#17
post #15

As much as I love bit manipulation, I have to say that most (not all) of them don't provide any real performance gain for C/C++ code (only some geeky satisfaction of writing difficult to interpret code!).. e.g. with any decent compiler... * if ((x & 1) == 0) performs same as, if ((x % 2) == 0) * if (x & (1 so on.. on the other hand, i find bit operations really handy when the variables in question are to be treated a…

[deleted]

Re: Low Level Bit Hacks You Must Know

#18

Here's a much more comprehensive collection of bit hacks: http://graphics.stanford.edu/~seander/bithacks.html

Ha. Next time someone asks how to swap two values at an interview question (I think this is pretty common):

  #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))

Re: Low Level Bit Hacks You Must Know

#19
post #15

As much as I love bit manipulation, I have to say that most (not all) of them don't provide any real performance gain for C/C++ code (only some geeky satisfaction of writing difficult to interpret code!).. e.g. with any decent compiler... * if ((x & 1) == 0) performs same as, if ((x % 2) == 0) * if (x & (1 so on.. on the other hand, i find bit operations really handy when the variables in question are to be treated a…

x & (1Take x = 2 and n = 1

  x & (1 
fwiw, you were actually looking for (x & ((1 That aside, you're absolutely right. Using bit ops is almost always a bad idea. It turns a simple store (to a byte-addressed memory address) into a read, manipulate, and store (to a bit within a byte-addressed memory address) which can have pretty bad side affects in any concurrent environment.

Re: Low Level Bit Hacks You Must Know

#20
post #10

Is working on embedded systems really this fun? I might have to change careers...

It was 10-20 years ago. The funnest project I ever worked on was to implement a 17-function motor controller with 40-bit precision onto an MCU with 2K of RAM and 64 BYTES of RAM. That was in '93. Today embedded systems generally run Linux. You get to write a bit of assembly language code in your bootloader, and then it's just bog standard Unix programming. It's even likely that you'll be doing most of your coding in…

There are still a lot of 8051s and MSP430s out there.

The fun thing about embedded is that since everything is done as a result of interrupts or clock signals it's all the joys of multitasking without a threading library.

I just got handed a project where the 'app' was just main() { while(1) {} } ! Everything happens as the result of functions that get magically called when certain bit patterns appear

Post reply on HN