English Amiga Board

English Amiga Board (http://eab.abime.net/index.php)
-   Coders. Blitz Basic (http://eab.abime.net/forumdisplay.php?f=126)
-   -   [blitz] need a small translation from c (http://eab.abime.net/showthread.php?t=99390)

peceha 27 October 2019 20:26

[blitz] need a small translation from c
 
Hi,
Code:

SerReq->io_SerFlags &= ~SERF_PARTY_ON
SerReq->io_SerFlags |= SERF_XDISABLED

is it like:
Code:

*sereq\io_SerFlags = *sereq\io_SerFlags & NOT #SERF_PARTY_ON
*sereq\io_SerFlags = *sereq\io_SerFlags | #SERF_XDISABLED

?

Thanks

thomas 27 October 2019 22:15

No, these are bit-wise operations, not boolean.

x &= ~y
clears every bit in x that is set in y

x |= y
sets every bit in x that is set in y.

Daedalus 27 October 2019 22:17

That looks correct to me anyway, but I haven't tested it...

Edit: Hmmm, maybe my brain's tired now, but thomas' post has me thinking. I still can't see that it's any different - & and | operate in a bitwise fashion in Blitz.

thomas 27 October 2019 22:25

Is NOT bitwise, too? Then what would be the boolean "not" operator in Blitz?

In C ^ is the boolean not, ~ is bitwise.

Daedalus 27 October 2019 22:32

yes, NOT is bitwise too. It works in boolean expressions because true is accepted to be any non-zero value, but is always returned as -1. So a bitwise NOT of -1 will equate to 0 / false. So:

Code:

If NOT x = y Then NPrint "Not equal"
Works because x = y is evaluated as -1 or 0, as does
Code:

NPrint Bin$(NOT %10000000)

thomas 28 October 2019 09:48

Quote:

Originally Posted by Daedalus (Post 1354621)
NOT is bitwise too. It works in boolean expressions because true is accepted to be any non-zero value

This is a contradiction in itself. If any non-zero value is true, then 2 is true, too. If NOT is bitwise, then NOT 2 is -3 which is still true. So NOT does not work in boolean expressions. It only works if TRUE (a.k.a. -1) has been returned by a comparison.

Daedalus 28 October 2019 13:13

When you put it like that, indeed, but my point was made in the rest of the sentence after that:
Quote:

, but is always returned as -1
Boolean expressions are always returned to the evaluation as true or false. If the terms you NOT are just boolean, then it's a boolean operation. If you try to NOT anything non-boolean, then it's bitwise. Context is key.

Hedeon 28 October 2019 14:48

Quote:

Originally Posted by thomas (Post 1354618)
In C ^ is the boolean not, ~ is bitwise.

What is ! and what is 'exclusive or' bitwise? I am still learning but I thought ^ was something else?

hooverphonique 28 October 2019 15:35

Quote:

Originally Posted by Hedeon (Post 1354753)
What is ! and what is 'exclusive or' bitwise? I am still learning but I thought ^ was something else?

in C, ! is logical not, ^ is bitwise xor.

Hedeon 28 October 2019 17:41

So NOT could be bitwise, boolean and logical? ok.

hooverphonique 28 October 2019 18:27

Quote:

Originally Posted by Hedeon (Post 1354791)
So NOT could be bitwise, boolean and logical? ok.

What is referred to as 'boolean' above is the equivalent of my 'logical', I think. Boolean logic applies to both bitwise and logical operators, so it may be a bit misleading calling one of them 'boolean'.

Daedalus 28 October 2019 18:48

Yeah, the names can be a little confusing. Boolean is a *type* of logic, where you can only have two values, true or false, 1 or 0. A boolean variable is one which is essentially 1 bit. In computing terms, "logical" is usually used to describe operations where each entire term is taken as boolean, so each value can be either true or false, regardless of what arrangement of bits they actually contain. "Bitwise" is used to refer to operations that focus on the individual bits of a value, rather than what it represents overall. C has explicit operators that differentiate the two, ! is (boolean) logical NOT and ~ is bitwise NOT. Blitz doesn't really have that same concept of boolean values, and so internally represents true as -1 and false as 0. So something like
Code:

NPrint 5>3
will produce -1 as the output, because 5 is greater than 3. The reason it does this is that -1 (true) is represented in binary as all ones (%11111111), and 0 (false) is represented as all zeroes (%00000000). Thus, doing a bitwise NOT (flipping every bit in the value) swaps true to false and false to true. So
Code:

NPrint NOT 5>3
will output 0.
This works for all Blitz operations that are boolean in nature, however it does mean that Blitz can't check for a zero value by doing e.g. NOT filehandle as is common in C (!filehandle). Instead, filehandle <> 0 is used.


All times are GMT +2. The time now is 01:09.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.

Page generated in 0.04344 seconds with 11 queries