대부분의 경우 Erlang은 목록과 재귀를 사용하므로 각 비트를 반복적으로 인덱싱하려고 시도하는 것이 아니라 개별적으로 각 비트를 통과하는 것이 관례입니다.
The documentation on how to work with bitstrings is here. 숫자 21에서 비트 문자열을 만들려면 <<21>>
을 쓸 수 있습니다. 바이너리의 멤버의 디폴트 타입은 integer이며, 정수의 디폴트 사이즈는 8이므로, 00010101
과 같은 비트 스트링을 생성 할 것이다. 값의 마지막 N 바이트를 구체적으로 가져 오려면 <<Value:N>>
입니다. 21의 마지막 5 비트를 얻기 위해 을 산출 할 <<21:5>>
이라고 말할 수 있습니다. 이 목록의 비트 또는 항목이 부족할 때
-module(bitmask).
-export([get_subarray_from_bitarray/2]).
get_subarray_from_bitarray(Bitstring, List) ->
get_subarray_from_bitarray_loop(Bitstring, List, []).
get_subarray_from_bitarray_loop(_Bits, [], Gathered) ->
io:format("End of list~n", []),
lists:reverse(Gathered);
get_subarray_from_bitarray_loop(<<>>, _Others, Gathered) ->
io:format("End of bitstring~n", []),
lists:reverse(Gathered);
get_subarray_from_bitarray_loop(<<Bit:1, Rest/bitstring>>, [Item | Others], Gathered) ->
io:format("Bit: ~w ~n", [Bit]),
case Bit of
1 -> get_subarray_from_bitarray_loop(Rest, Others, [Item | Gathered]);
0 -> get_subarray_from_bitarray_loop(Rest, Others, Gathered)
end.
첫 번째 2 절
최종 목록을 반환 :
은 당신이 원하는 일을하기 위해 다음과 같은 모듈을 썼다. 중요한 비트 구문은 마지막 절인
<<Bit:1, Rest/bitstring>>
의 머리 부분에 있습니다. 이렇게하면
Bit
의 값이 비트 문자열의 첫 번째 비트 값으로 설정되고
Rest
이 나머지 비트 문자열 값으로 설정됩니다. Bit 값에 따라 현재 항목을 목록에 추가할지 여부를 결정합니다.
예 호출 아래 :
> bitmask:get_subarray_from_bitarray(<<21:5>>, [1, 3, 5, 42, 23]).
Bit: 1
Bit: 0
Bit: 1
Bit: 0
Bit: 1
End of list
[1,5,23]
> bitmask:get_subarray_from_bitarray(<<31>>, [1, 3, 5, 42, 23]).
Bit: 0
Bit: 0
Bit: 0
Bit: 1
Bit: 1
End of list
[42,23]
> bitmask:get_subarray_from_bitarray(<<5:3>>, [1, 3, 5, 42, 23]).
Bit: 1
Bit: 0
Bit: 1
End of bitstring
[1,5]