Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QBJS v0.6.0 Release
#21
I see it translates to a short-circuiting OR instead of a bitwise OR:

Code: (Select All)
await QB.sub_Print([ 10 ||  20]);
 
temp fix:

Code: (Select All)
print 10 or 20

dim z
$if Javascript then
    z = 10 | 20
$end if

print z
Reply
#22
(11-10-2022, 01:46 PM)a740g Wrote: I have discovered that the following snippet in QBJS outputs different results compared to QB64-PE.

Code: (Select All)
Print 10 + 20
Print 10 Or 20

In QB64-PE, it prints
30
30

And in QBJS, it prints
30
10
This is always going to be a pain in BASIC only because "OR" the conditional operator was never distinguished cleanly from "OR" the logical operator. For example in C you know it's "||" for the former and a single pipe for the latter. It's distinguishable in Purebasic with the word for conditional and the symbol for logical. But for QB64(PE) and whatever made like M$'s brand of BASIC, and other such dialects, we'll have to deal with the interpreter trying to figure out what is conditional and what is logical, without it needing to be in an "IF" statement. Attempts were made, I'm sure to try to force the conditional thing only in "IF" but many programmers resisted, so it is the way it is.

Anyway the solution should be use "&" for "AND" and "|" for "OR", in an example such as this one, and leave the words for conditional comparison. But it won't be done because "I can't run it in QBasic now and what am I going to do?"

Another thing is that arithmetic and logic don't interlock that well because the former tends to assume base-10 while the latter is base-2, and we're trying to write everything in base-10.

I ninja'ed @vince who provided a good solution. Good to know QBJS is flexible enough!

EDIT: Used incorrect terms, "logical" should be "bitwise", and "conditional" might be the same as "logical" but otherwise everything here is correct.
Reply
#23
(11-10-2022, 01:46 PM)a740g Wrote: @dbox I use QBJS to try out ideas and code that I end up using elsewhere. It is an excellent resource for me that way. Thank you.

I have discovered that the following snippet in QBJS outputs different results compared to QB64-PE.

Code: (Select All)
Print 10 + 20
Print 10 Or 20

In QB64-PE, it prints
30
30

And in QBJS, it prints
30
10

Yes, this is something I've gone back and forth on.  QBasic really only has bitwise operators, where Javascript has both bitwise and logical operators.  So, at present the only true bitwise operator in QBJS is XOR.  All other operators are being converted to their logical equivalents in JS.  I like being able to take advantage of shortcut evaluation in conditional statements, but it does present a compatibility challenge.
Reply
#24
(11-10-2022, 03:46 PM)dbox Wrote: Yes, this is something I've gone back and forth on.  QBasic really only has bitwise operators, where Javascript has both bitwise and logical operators.  So, at present the only true bitwise operator in QBJS is XOR.  All other operators are being converted to their logical equivalents in JS.  I like being able to take advantage of shortcut evaluation in conditional statements, but it does present a compatibility challenge.

Correct. We are also thinking about Boolean short-circuiting versions of AND and OR. See Boolean short-circuiting versions of `AND` and `OR` · Issue #211 · QB64-Phoenix-Edition/QB64pe (github.com)

I think you should keep the legacy functionality for AND and OR and use something else for Boolean short-circuiting versions of AND and OR (like AndAlso and OrElse). Would be nice to see this implemented in QBJS.
Reply
#25
(11-10-2022, 04:30 PM)a740g Wrote: Correct. We are also thinking about Boolean short-circuiting versions of AND and OR. See Boolean short-circuiting versions of `AND` and `OR` · Issue #211 · QB64-Phoenix-Edition/QB64pe (github.com)

I think you should keep the legacy functionality for AND and OR and use something else for Boolean short-circuiting versions of AND and OR (like AndAlso and OrElse). Would be nice to see this implemented in QBJS.

I'll definitely look into this further.  The other wrinkle that makes it interesting to support is the fact that we are essentially mapping a strongly typed language onto a weakly typed one, because of this sometimes the results of bitwise operations can be... unexpected.

Interesting that you are looking at adding logical operators for QB64.  This is something that bit me several times a few years back when I was getting back into QB until I remembered there was no short-circuiting.  Is this planned for inclusion in the next QBPE release or still in the review phase?
Reply
#26
(11-10-2022, 06:24 PM)dbox Wrote: Interesting that you are looking at adding logical operators for QB64.  This is something that bit me several times a few years back when I was getting back into QB until I remembered there was no short-circuiting.  Is this planned for inclusion in the next QBPE release or still in the review phase?

Review phase. We have a pile of issues to work out on the PE GitHub and some of those need to be addressed first.
Reply
#27
(11-10-2022, 01:46 PM)a740g Wrote: @dbox I use QBJS to try out ideas and code that I end up using elsewhere. It is an excellent resource for me that way. Thank you.

I have discovered that the following snippet in QBJS outputs different results compared to QB64-PE.

Code: (Select All)
Print 10 + 20
Print 10 Or 20

In QB64-PE, it prints
30
30

And in QBJS, it prints
30
10

I'm pretty sure that you'll find QBJS more compatible with QB64PE 99.9% of the time, but for the occasional oddball things that BAM can handle:

BAM:

   
Reply
#28
Nice to know BAM has both "OR"s in the water.

Pete Big Grin
Reply
#29
A logical AND, OR and NOT would be useful. -- Regarding the binary system: does anyone know why the following program returns (Not 10) And (Not 20) a result of -31?

Code: (Select All)
'Beispiel logische Operatoren, QuickBasic 4, Seite 3.10
'10. Nov. 2022

Dim As Integer a, b

a = 10
b = 20

Print: Print Tab(4); "a And b"
Print Tab(4); a And b

'Eine 1 ergibt sich nur, wenn beide Bits 1 sind.
'00001010 -> 10
'00010100 -> 20
'--------
'00000000

Print: Print Tab(4); "a Or b"
Print Tab(4); a Or b

'Eine 1 ergibt sich, wenn mindestens ein Bit eine 1 ist.
'00001010 -> 10
'00010100 -> 20
'--------
'00011110 -> 30 (16+8+4+2)

Print: Print Tab(4); "Not"
Print Tab(4); Not a '-> -11
Print Tab(4); Not b '-> -21

'Das Ausfuehren von NOT entspricht der Addition
'einer 1 zur Zahl mit anschliessender Negation

Print: Print Tab(4); "(Not a) And (Not b)"
Print Tab(4); (Not a) And (Not b) '-> -31

'-10 = 1000 1010
'-20 = 1001 0100
'---------------
'(1)1 001 1111 -> -31

'Klappt nur, wenn 0 + 0 = 1
'Addition zweier negativer Zahlen im Binaerformat

End
Reply
#30
Code: (Select All)
$if Javascript then
    a = 10
    b = 20
    z = (~a) & (~b)
$end if
print z

please review Steve's useful tutorial on bitwise operations 
https://qb64phoenix.com/forum/showthread.php?tid=305
Reply




Users browsing this thread: 6 Guest(s)