zero length segments!

Happi <>
Tue Sep 24 16:40:53 CEST 2002


The bitsize can not be an arbitrary expression, just bind the size before
the match:

t(Bit) when integer(Bit) ->
  S = (31 - Bit),
   <<_:S, Flag:1, _:(Bit)>> = <<1:32>>,
   Flag.

----- Original Message -----
From: "Vance Shipley" <>
To: <>
Sent: Tuesday, September 24, 2002 4:02 PM
Subject: Re: zero length segments!


>
> Well didn't I speak too soon!  While this works wonderfully in the shell
> it won't complile. :(
>
>
> Eshell V5.1.2  (abort with ^G)
> 1> T = fun(Bit) -> <<_:(31 - Bit), Flag:1, _:(Bit)>> = <<1:32>>, Flag end.
> #Fun<erl_eval.5.63972567>
> 2> T(0).
> 1
> 3> T(1).
> 0
>
> -module(t).
> -export([t/1]).
>
> t(Bit) when integer(Bit) ->
>    <<_:(31 - Bit), Flag:1, _:(Bit)>> = <<1:32>>,
>    Flag.
>
> 1> c(t).
> ./t.erl:5: illegal bit size
> error
>
>
> The problem seems to be with the expression (31 - Bit).
>
> Any idea how to get past this?
>
> -Vance
>
> Vance Shipley
> Motivity Telecom Inc.
> +1 519 579 5816
> 
>
>
> On Tue, Sep 24, 2002 at 06:33:23AM -0400, Vance Shipley wrote:
> >
> > Was I ever happily suprised when I found that this was a valid
expression:
> >
> > <<0:8, 1:24, 0:0>>
> >
> > It results in:
> >
> > <<0,0,0,1>>
> >
> > The reason this is so good is that it I don't have to handle a special
> > case when X=0:
> >
> > Flag = <<_:(31-X), Flag:1, _:(X)>>
> >
> > Of course I'm sure that that is why it works, I'm just very happy about
it. :)
> >
> > -Vance
>




More information about the erlang-questions mailing list