Live data from Hacker News

Low Level Bit Hacks You Must Know

catonmat.net

31–40 of 49 posts

Re: Low Level Bit Hacks You Must Know

#31
post #30
post #29

Earlier quoted context omitted.

This looks magical :-) Are you sure you're decoding bits from an octet stream (normal 8-bit bytes) and not using logical booleans? I ask this because I don't see any bit-manipulation operators, except maybe for "1 :> 'T" which looks like the SML module type casting, but I could be wrong.

Yes very sure. nthbit will extract the nth bit from an integer type. The bitwise logic is the &&& and http://msdn.microsoft.com/en-us/library/dd469495.aspx bitstream turns a sequence of integer types into a sequence of bits. So if you wanted to turn a stream into bits you'd do something like the following. new System.IO.FileStream('foo.txt') |> Seq.unfold (fun s -> (s , match s.read with | -1 -> None | x -> Some(x)))…

Just for fun here's the reverse to turn a sequence of booleans into a sequence of integer types (byte,int,long,etc). Note that the length of sequence s must be a multiple of sizeof times 8

  let bitsToBitContainer (s:seq) : seq =
    let sz = sizeof*8
    s
    |> Seq.scan (fun (i,n) x -> (i++,(n Seq.filter (fun (i,n) -> i = sz)
    |> Seq.map (fun (i,n) -> n)

Re: Low Level Bit Hacks You Must Know

#32

Earlier quoted context omitted.

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

Look into Esterel for some theoretical grounding. It will help you reason about reactive systems.

Re: Low Level Bit Hacks You Must Know

#33

Earlier quoted context omitted.

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)))

Is it pretty common? As someone who does a fair number of software engineer interviews, that's a trick question. The real answer to "swap two vars with no temps" is: 1. Don't be clever in our code base. Use a temp variable. 2. There's various dumb tricks with XOR, and possibly add/subtract if overflows don't break. 3. A sequence of several instructions where each of them requires the result of the previous one may no…

Embedded and firmware engineering questions expect this as an answer. Anyone following your advice will not be taken seriously. This is true regardless of whether you're correct factually. Readers of this thread deserve to know that.

Re: Low Level Bit Hacks You Must Know

#34
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…

It's my impression that this sort of "fun" work is still done in firmware engineering at Broadcom for instance.

Re: Low Level Bit Hacks You Must Know

#35
post #31
post #30

Earlier quoted context omitted.

Yes very sure. nthbit will extract the nth bit from an integer type. The bitwise logic is the &&& and http://msdn.microsoft.com/en-us/library/dd469495.aspx bitstream turns a sequence of integer types into a sequence of bits. So if you wanted to turn a stream into bits you'd do something like the following. new System.IO.FileStream('foo.txt') |> Seq.unfold (fun s -> (s , match s.read with | -1 -> None | x -> Some(x)))…

Just for fun here's the reverse to turn a sequence of booleans into a sequence of integer types (byte,int,long,etc). Note that the length of sequence s must be a multiple of sizeof times 8 let bitsToBitContainer (s:seq ) : seq = let sz = sizeof *8 s |> Seq.scan (fun (i,n) x -> (i++,(n Seq.filter (fun (i,n) -> i = sz) |> Seq.map (fun (i,n) -> n)

I am liking this F# thing. Thanks! :-)

Re: Low Level Bit Hacks You Must Know

#37
post #33

Earlier quoted context omitted.

Is it pretty common? As someone who does a fair number of software engineer interviews, that's a trick question. The real answer to "swap two vars with no temps" is: 1. Don't be clever in our code base. Use a temp variable. 2. There's various dumb tricks with XOR, and possibly add/subtract if overflows don't break. 3. A sequence of several instructions where each of them requires the result of the previous one may no…

Embedded and firmware engineering questions expect this as an answer. Anyone following your advice will not be taken seriously. This is true regardless of whether you're correct factually. Readers of this thread deserve to know that.

3 is actually a quite valid point for embedded. Any swap actually will be expensive when it comes to keeping cache lines clean. The correct answer in that case is just to not swap the variables, and instead swap their uses later on:

    int x, y;
    ...
    SWAP(x, y);
    foo(x, y);
becomes

    int x, y;
    ...
    foo(y, x);
(naturally, this is why I still eagerly await the arrival of a C compiler that has macros with LISP power)

Re: Low Level Bit Hacks You Must Know

#38
post #37
post #33

Earlier quoted context omitted.

Embedded and firmware engineering questions expect this as an answer. Anyone following your advice will not be taken seriously. This is true regardless of whether you're correct factually. Readers of this thread deserve to know that.

3 is actually a quite valid point for embedded. Any swap actually will be expensive when it comes to keeping cache lines clean. The correct answer in that case is just to not swap the variables, and instead swap their uses later on: int x, y; ... SWAP(x, y); foo(x, y); becomes int x, y; ... foo(y, x); (naturally, this is why I still eagerly await the arrival of a C compiler that has macros with LISP power)

In that case, you can get the right behavior by just swapping the variables using a temporary variable. If the compiler is decent, it'll automatically swap their uses later on.

I don't know how every compiler works, but if you use Clang (or anything LLVM-based), it converts everything to Single Static Assignment (SSA) form:

    int x1 = 42, y1 = 666;
    ...
    int tmp = x1; x2 = y1; y2 = tmp;  // SWAP(x, y)
    foo(x2, y2);
In SSA form, the value of a variable does not change, so it ends up creating a bunch of "imaginary" variables to hold intermediate values. From there, it does optimizations, then figures out how best to allocate registers, and what needs to be stack-allocated.

Re: Low Level Bit Hacks You Must Know

#39
post #38
post #37

Earlier quoted context omitted.

3 is actually a quite valid point for embedded. Any swap actually will be expensive when it comes to keeping cache lines clean. The correct answer in that case is just to not swap the variables, and instead swap their uses later on: int x, y; ... SWAP(x, y); foo(x, y); becomes int x, y; ... foo(y, x); (naturally, this is why I still eagerly await the arrival of a C compiler that has macros with LISP power)

In that case, you can get the right behavior by just swapping the variables using a temporary variable. If the compiler is decent, it'll automatically swap their uses later on. I don't know how every compiler works, but if you use Clang (or anything LLVM-based), it converts everything to Single Static Assignment (SSA) form: int x1 = 42, y1 = 666; ... int tmp = x1; x2 = y1; y2 = tmp; // SWAP(x, y) foo(x2, y2); In SSA…

Cool, thanks. I know far too little about compiler optimizations, it's always nice to hear about them.

Re: Low Level Bit Hacks You Must Know

#40
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.

Perhaps you are using a microcontroller with memory-mapped control registers?
Post reply on HN