Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binary format, bitwise operations exist? eg. <<16#7F, 16#FF>> bsl 1

Tags:

binary

erlang

xor

In erlang, there are bitwise operations to operate on integers, for example:

1&gt  127 bsl 1.
254

there is also the ability to pack integers into a sequence of bytes

&lt&lt 16#7F, 16#FF &gt&gt

is it possible, or are there any operators or BIFs that can perform bitwise operations (eg AND, OR, XOR, SHL, SHR) on binary packed data?

for example (if bsl worked on binary packages - which it does not):

1&gt  &lt&lt 16#7F, 16#FF &gt&gt bsl 1.
&lt&lt 255, 254 &gt&gt
like image 347
Mike Hamer Avatar asked Dec 24 '08 02:12

Mike Hamer


1 Answers

Try out this way:

bbsl(Bin,Shift) -> <<_:Shift,Rest/bits>> = Bin, <<Rest/bits,0:Shift>>.
like image 177
Hynek -Pichi- Vychodil Avatar answered Nov 11 '22 10:11

Hynek -Pichi- Vychodil