Using Erlang's unbounded integer sizes we can accomplish this:
1> Bits = <<16#0FFFFFFF:(4*8)>>.
<<15,255,255,255>>
2> size(Bits).
4
3> Size=size(Bits)*8.
32
4> <<Num:Size>> = Bits.
<<15,255,255,255>>
5> Num.
268435455
6> Num2 = Num bsl 4.
4294967280
7> Bits2 = <<Num2:Size>>.
<<"ÿÿÿð">>
8> <<A:8,B:8,C:8,D:8>>=Bits2.
<<"ÿÿÿð">>
9> A.
255
10> D.
240
as we expected.
Note that in my solution I anticipated how many shifts I would need (4) by adding 4 '0's to the initial string stored in the binary (16#0F... the first 4 positions are 0)
Not sure how I would handle it if I had to shift beyond the 'boundary' of the binary container, I guess you would just AND with 2^Size-1.
Mike Hamer
2008-12-24 04:05:12