[erlang-questions] Bit-level binaries broken in R11B-5?

Bob Ippolito bob@REDACTED
Sun Sep 16 03:12:19 CEST 2007


I was playing around with bit-level binaries in R11B-5 to see if we
could use them to make some of our code shorter and faster, however it
seems that some of the operations that we'd want to do give very
unexpected results. Here's a short test module that uses two different
methods of concatenating N bit long binaries and ends up with mostly
the incorrect results.

-module(bits).
-compile([bitlevel_binaries, binary_comprehension]).
-export([test/0]).

test() ->
    lists:map(fun test/1, lists:seq(0, 8)).

test(Bits) ->
    A = <<1:Bits>>,
    B = <<1:Bits>>,
    Expect = <<(1 bsl Bits bor 1):(2 * Bits)>>,
    R1 = <<A/binary, B/binary>>,
    R2 = <<<<N/binary>> || N <- [A, B]>>,
    io:format("Bits: ~p~n", [Bits]),
    io:format("  Expect : ~p~n", [Expect]),
    io:format("  Concat : ~p~n", [R1]),
    io:format("  BinComp: ~p~n", [R2]),
    io:format("~n"),
    (Expect =:= R1) and (Expect =:= R2).

%%%

And the output:

Erlang (BEAM) emulator version 5.5.5 [source] [smp:2]
[async-threads:4] [kernel-poll:true]
(on Mac OS X 10.4.10, Core 2 Duo MacBook Pro).

Bits: 0
  Expect : <<>>
  Concat : <<>>
  BinComp: <<>>

Bits: 1
  Expect : <<3:2>>
  Concat : <<2:2>>
  BinComp: <<0:2>>

Bits: 2
  Expect : <<5:4>>
  Concat : <<2:4>>
  BinComp: <<0:4>>

Bits: 3
  Expect : <<9:6>>
  Concat : <<10:6>>
  BinComp: <<9:6>>

Bits: 4
  Expect : <<17>>
  Concat : <<>>
  BinComp: <<"\"">>

Bits: 5
  Expect : <<8,1:2>>
  Concat : <<164,0:2>>
  BinComp: <<41,1:2>>

Bits: 6
  Expect : <<4,1:4>>
  Concat : <<164,0:4>>
  BinComp: <<100,10:4>>

Bits: 7
  Expect : <<2,1:6>>
  Concat : <<40,0:6>>
  BinComp: <<40,20:6>>

Bits: 8
  Expect : <<1,1>>
  Concat : <<1,1>>
  BinComp: <<1,1>>

[true,false,false,false,false,false,false,false,true]


I get the same results on FreeBSD x86 with or without HiPE. I haven't
bothered to test on Linux but I assume it's broken just the same.

-bob



More information about the erlang-questions mailing list