[erlang-questions] Defined macros and logical bitwise operations
Brujo Benavides
elbrujohalcon@REDACTED
Fri Jun 7 13:14:05 CEST 2019
Hi Sergei,
Just to be clear, your issue does not depend on the usage of macros.
See below…
—————————————————————————————————————————————————————————
-module test.
-export [print/0].
print() ->
io:format("NUM1: ~p~n", [2#01]),
io:format("NUM2: ~p~n", [2#01 bsl 1]),
io:format("NUM_BOR_PAR: ~p~n", [(2#01) bor (2#01 bsl 1)]),
io:format("NUM_BOR_NO_PAR: ~p~n", [2#01 bor 2#01 bsl 1]),
io:format("NUM_BAND_PAR: ~p~n", [(2#01) band (2#01 bsl 1)]),
io:format("NUM_BAND_NO_PAR: ~p~n", [2#01 band 2#01 bsl 1]),
io:format("NUM_BXOR_PAR: ~p~n", [(2#01) bxor (2#01 bsl 1)]),
io:format("NUM_BXOR_NO_PAR: ~p~n", [2#01 bxor 2#01 bsl 1]),
ok.
—————————————————————————————————————————————————————————
1> c(test).
{ok,test}
2> test:print().
NUM1: 1
NUM2: 2
NUM_BOR_PAR: 3
NUM_BOR_NO_PAR: 2
NUM_BAND_PAR: 0
NUM_BAND_NO_PAR: 2
NUM_BXOR_PAR: 3
NUM_BXOR_NO_PAR: 0
ok
—————————————————————————————————————————————————————————
Your issue is predicated entirely on the operator precedence and you can find documentation about it here:
http://erlang.org/doc/reference_manual/expressions.html#operator-precedence <http://erlang.org/doc/reference_manual/expressions.html#operator-precedence>
The important bit there is:
+ - bor bxor bsl bsr or xor Left associative
That means that X band Y bsl Z is not the same as (X band (Y bsl Z)) but ((X band Y) bsl Z).
Hope this helps :)
Cheers!
Brujo Benavides <http://about.me/elbrujohalcon>
> On 7 Jun 2019, at 08:03, DOBRO Sergei <chessvegas@REDACTED> wrote:
>
> -module(test).
>
> -export([print/0]).
>
> -define(NUM1, 2#01).
> -define(NUM2, ?NUM1 bsl 1).
>
> -define(NUM_BOR_PAR, (?NUM1) bor (?NUM2)).
> -define(NUM_BOR_NO_PAR, ?NUM1 bor ?NUM2).
>
> -define(NUM_BAND_PAR, (?NUM1) band (?NUM2)).
> -define(NUM_BAND_NO_PAR, ?NUM1 band ?NUM2).
>
> -define(NUM_BXOR_PAR, (?NUM1) bxor (?NUM2)).
> -define(NUM_BXOR_NO_PAR, ?NUM1 bxor ?NUM2).
>
> print() ->
> io:format("NUM1: ~p~n", [?NUM1]),
> io:format("NUM2: ~p~n", [?NUM2]),
> io:format("NUM_BOR_PAR: ~p~n", [?NUM_BOR_PAR]),
> io:format("NUM_BOR_NO_PAR: ~p~n", [?NUM_BOR_NO_PAR]),
> io:format("NUM_BAND_PAR: ~p~n", [?NUM_BAND_PAR]),
> io:format("NUM_BAND_NO_PAR: ~p~n", [?NUM_BAND_NO_PAR]),
> io:format("NUM_BXOR_PAR: ~p~n", [?NUM_BXOR_PAR]),
> io:format("NUM_BXOR_NO_PAR: ~p~n", [?NUM_BXOR_NO_PAR]),
> ok.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190607/d84ecfe9/attachment.htm>
More information about the erlang-questions
mailing list