Bit Syntax Problem

Matthias Lang matthias@REDACTED
Tue Mar 25 08:37:39 CET 2003


Edwin Zacharias writes:
 > I'm having a problem with the bit syntax in the
 > following function:
 > 
 > g() ->
 >   case <<0>> of
 >     <<0:1, _/binary>> -> true;
 >     _ -> false
 >   end.
 > 
 > This should return true, but it returns false when I
 > run it.  However when I run just the body of the
 > function in erl, it returns true.  Is there something
 > wrong with my function declaration?

The pattern <<0:1, _/binary>> is not allowed; the "unit" for binaries
is 8 bits, which means that the whole expression will be a multiple of
N*8 + 1 bits. That's not allowed.

What you probably want to do is

  h() ->
    case <<0>> of
       <<0:1, _:7>> -> true;
       -> false
    end.

the compiler could be a little more helpful by giving you a warning,
but it doesn't.

References:

  http://www.erlang.org/doc/r9b/doc/extensions/bit_syntax.html
 
  (in particular 6.6, which mentions a case similar to yours)

Matthias



More information about the erlang-questions mailing list