Defined macros and logical bitwise operations

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Defined macros and logical bitwise operations

DOBRO
Hi all,
 
I ran into the issue regarding defined macros and bitwise operators bor/band/bxor.
The output result depends on whether the parentheses are set or not.
 
The code below shows this behaviour:
 
-------------------------------------------------------------------------------------------------------------------
-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.
 
-------------------------------------------------------------------------------------------------------------------
 
In shell:
 
$ erl
Erlang/OTP 22 [erts-10.4.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
 
Eshell V10.4.1  (abort with ^G)
 
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
 
-----------------------------------------
 
Is there any explanation of such syntax in the OTP docs?
 
I'm using OTP 22.0.2
 

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Defined macros and logical bitwise operations

Brujo Benavides-3
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:

The important bit there is: 
+ - bor bxor bsl bsr or xorLeft 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!



On 7 Jun 2019, at 08:03, DOBRO Sergei <[hidden email]> 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.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Defined macros and logical bitwise operations

DOBRO
Hi Brujo,
 
now I'm coming to an answer why shell gives me the constant result today:
 
$ erl
Erlang/OTP 22 [erts-10.4.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
 
Eshell V10.4.1  (abort with ^G)
1> calendar:day_of_the_week(date()).
heavy_friday
 
 
 
07.06.2019, 14:13, "Brujo Benavides" <[hidden email]>:
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:
 
The important bit there is: 
+ - bor bxor bsl bsr or xorLeft 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!
 
 
On 7 Jun 2019, at 08:03, DOBRO Sergei <[hidden email]> 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.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Defined macros and logical bitwise operations

DOBRO
In reply to this post by Brujo Benavides-3
... and thanks a lot! You saved me :)
 
07.06.2019, 14:13, "Brujo Benavides" <[hidden email]>:
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:
 
The important bit there is: 
+ - bor bxor bsl bsr or xorLeft 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!
 
 
On 7 Jun 2019, at 08:03, DOBRO Sergei <[hidden email]> 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.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions