[erlang-questions] subnet masking with binary matching
Kostis Sagonas
kostis@REDACTED
Thu Jan 18 09:01:08 CET 2007
Bob Ippolito wrote:
>
> You could just do this:
>
> mask_address(Addr, Maskbits) ->
> B = list_to_binary(tuple_to_list(Addr)),
> io:format("address as binary: ~p ~p~n", [B,Maskbits]),
> Rest = (size(B) * 8) - Maskbits,
> <<Subnet:Maskbits, _Host:Rest>> = B,
> Subnet.
>
> If/when the feature enhancements proposed in that paper end up in
> Erlang, then this code would be shorter and cleaner. It sure would
> make a huge difference in a lot of my code, but even the current
> syntax is much better than bit twiddling other languages.
I second the last sentence.
By the way, the feature enhancements proposed in our paper are already
in the development version of Erlang/OTP and will most probably be
released as part of R11B-3.
Jeff's code would need to be slightly modified and use the "bitstr" type
specifier:
mask_address(Addr, Maskbits) ->
B = list_to_binary(tuple_to_list(Addr)),
io:format("address as binary: ~p ~p~n", [B,Maskbits]),
<<Subnet:Maskbits, _Host/bitstr>> = B,
Subnet.
Kostis
More information about the erlang-questions
mailing list