Obsidian | Level 7

Issue with Bitwise NOT (BNOT()) Function Result

Hello SAS Community,

I am currently facing an issue with the Bitwise NOT (BNOT) function in my SAS code. Instead of producing the expected inverse of 1 or 0, the function is returning random numbers in the result window.

I have tried using the BNOT function on binary values 1 and 0, but the output is not as expected. Has anyone encountered a similar problem or have any insights into what might be causing this issue?

Any help or suggestions would be greatly appreciated. Thank you in advance for your assistance!

```data Logical_Input;
input x y;
datalines;
0 0
0 1
1 0
1 1
;
run;

OPTION VALIDVARNAME=ANY;
data Logical_operation_table;
set Logical_Input;
"x and y"n	= BAND(x,y);
"x or y"n 	= BOR(x,y);
"x xor y"n 	= BXOR(x,y);
"not x"n 	= BNOT(x);
run;

proc print noobs; run;```

Output:

 x y x and y x or y x xor y not x 0 0 0 0 0 4294967295 0 1 0 1 1 4294967295 1 0 0 1 1 4294967294 1 1 1 1 0 4294967294

Best regards,

Vijay

1 ACCEPTED SOLUTION

Accepted Solutions
Meteorite | Level 14

Re: Issue with Bitwise NOT (BNOT()) Function Result

SAS has only 1 numerical type: number (take a look here: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p0dv87zb3bnse6n1mqo360be70qr.htm#:~:text...

and it keeps both integers and floating points in it on 8 bytes (64 bits). C keeps numbers also on 8 bytes but has different types so the same sequence of bits means different things depending on type.

Documentation say that bitwise functions operates on numbers in range from 0 to 2^32 - 1, and those numbers are represented by those red bits:

`bnotx=0100000111101111111111111111111111111111111000000000000000000000`

[EDIT:]

Also take a look at the log of the following:

``````data _null_;
x=1;
y=0;
band = Band(y,x);
bor  = Bor(y,x);
bnot = Bnot(y);

put x binary64. x best32.;
put y  binary64. y best32.;
put band binary64. band best32.;
put bor binary64. bor best32.;
put bnot binary64. bnot best32.;

z=2**32 - 1;
put z=;
run;``````

you will see that Bnot(0) is 2^32 - 1, so "full" negation.

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation

4 REPLIES 4
Meteorite | Level 14

Re: Issue with Bitwise NOT (BNOT()) Function Result

Run this:

``````data _null_;
____x=0;
bnotx = BNOT(____x);

put ____x= binary64.;
put bnotx= binary64.;
run;
``````
``````data _null_;
____x=1;
bnotx = BNOT(____x);

put ____x= binary64.;
put bnotx= binary64.;
run;``````

and look into the log so you will see which bits are flipped

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation

Obsidian | Level 7

Re: Issue with Bitwise NOT (BNOT()) Function Result

This is quite strange that we are getting these numbers but when using other functions like BAND we get a decimal number instead of a binary number.

Even in other programming languages like Python or C/C++ we get a number instead of binary digits or some random numbers in output.

Log:

```595  data _null_;
596    ____x=0;
597    bnotx = BNOT(____x);
598
599    put ____x= binary64.;
600    put bnotx= binary64.;
601  run;

____x=0000000000000000000000000000000000000000000000000000000000000000
bnotx=0100000111101111111111111111111111111111111000000000000000000000
NOTE: DATA statement used (Total process time):
real time           0.00 seconds
cpu time            0.00 seconds

602  data _null_;
603    ____x=1;
604    bnotx = BNOT(____x);
605
606    put ____x= binary64.;
607    put bnotx= binary64.;
608  run;

____x=0011111111110000000000000000000000000000000000000000000000000000
bnotx=0100000111101111111111111111111111111111110000000000000000000000
NOTE: DATA statement used (Total process time):
real time           0.00 seconds
cpu time            0.01 seconds```

Is there any way to convert these binary numbers back to 0 or 1 when using BNOT()

Meteorite | Level 14

Re: Issue with Bitwise NOT (BNOT()) Function Result

SAS has only 1 numerical type: number (take a look here: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p0dv87zb3bnse6n1mqo360be70qr.htm#:~:text...

and it keeps both integers and floating points in it on 8 bytes (64 bits). C keeps numbers also on 8 bytes but has different types so the same sequence of bits means different things depending on type.

Documentation say that bitwise functions operates on numbers in range from 0 to 2^32 - 1, and those numbers are represented by those red bits:

`bnotx=0100000111101111111111111111111111111111111000000000000000000000`

[EDIT:]

Also take a look at the log of the following:

``````data _null_;
x=1;
y=0;
band = Band(y,x);
bor  = Bor(y,x);
bnot = Bnot(y);

put x binary64. x best32.;
put y  binary64. y best32.;
put band binary64. band best32.;
put bor binary64. bor best32.;
put bnot binary64. bnot best32.;

z=2**32 - 1;
put z=;
run;``````

you will see that Bnot(0) is 2^32 - 1, so "full" negation.

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation

Super User

Re: Issue with Bitwise NOT (BNOT()) Function Result

What number did you expect?

If you have binary string 0001 and you flip all of the zeros to ones you get 1110 which is the number 14.

You do not get 0.

If you want to limit it to fixed number of bits use the MOD() function.

``````ndigits=1 ;
bnotx=mod(bnot(x),2**ndigits);``````

Let's look at all 16 possible values of 4 binary digits.

``````data test;
do x=0 to 7;
bnotx=bnot(x);
bnot4=mod(bnotx,2**4);
put / (3*x 3*bnotx 3*bnot4) ( binary4. +1 hex8. comma14. /);
end;
run;``````
```0000 00000000             0
1111 FFFFFFFF 4,294,967,295
1111 0000000F            15

0001 00000001             1
1110 FFFFFFFE 4,294,967,294
1110 0000000E            14

0010 00000002             2
1101 FFFFFFFD 4,294,967,293
1101 0000000D            13

0011 00000003             3
1100 FFFFFFFC 4,294,967,292
1100 0000000C            12

0100 00000004             4
1011 FFFFFFFB 4,294,967,291
1011 0000000B            11

0101 00000005             5
1010 FFFFFFFA 4,294,967,290
1010 0000000A            10

0110 00000006             6
1001 FFFFFFF9 4,294,967,289
1001 00000009             9

0111 00000007             7
1000 FFFFFFF8 4,294,967,288
1000 00000008             8
```

Discussion stats
• 4 replies
• 201 views
• 1 like
• 3 in conversation