BitScanForward

General FreeBASIC programming questions.
schooner
Posts: 30
Joined: Jul 27, 2015 20:53

Re: BitScanForward

Postby schooner » Nov 11, 2016 19:50

MrSwiss wrote:
schooner wrote:What is the reasoning for the -1?
-1 = TRUE -- 0 = FALSE (definition of FB-Boolean var.) as from version >= 1.04.0

MrSwiss,
Yes, but you have not really answered the question, only restated the definition. I suspect the negative logic might have something to do with a desired FreeBasic testing syntax:

Code: Select all

   if SSE then true         'SSE = -1
   if NOT SSE then false    'SSE = 0
MrSwiss
Posts: 3189
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: BitScanForward

Postby MrSwiss » Nov 11, 2016 20:11

schooner,
here comes my best guess, being the following:

- easier switching (as between 0 and +1)
- -1 = &hFF (a Byte), 0 = &h00
- so, a switch is simple by just using a NOT (binary bit-wise inversion)

Example:

Code: Select all

Dim As Boolean flag ' init. default is 0 = FALSE

If (some condition) Then flag = Not flag ' flag is now TRUE
schooner
Posts: 30
Joined: Jul 27, 2015 20:53

Re: BitScanForward

Postby schooner » Nov 11, 2016 20:58

MrSwiss wrote:schooner,
here comes my best guess, being the following:

- easier switching (as between 0 and +1)
- -1 = &hFF (a Byte), 0 = &h00
- so, a switch is simple by just using a NOT (binary bit-wise inversion)

Example:

Code: Select all

Dim As Boolean flag ' init. default is 0 = FALSE

If (some condition) Then flag = Not flag ' flag is now TRUE

Looks good!
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: BitScanForward

Postby MichaelW » Nov 11, 2016 21:43

schooner wrote:What is the reasoning for the -1?

For FreeBASIC, like the Microsoft QuickBASIC it is based on, the operators AND, OR, NOT, and XOR are bitwise operators.

I suspect that the -1 value for TRUE was to ensure that NOT TRUE would produce FALSE.

To illustrate, using 8-bit signed integers, and assuming that TRUE = -1 (11111111b), and noting that a bitwise NOT operator inverts the bit values, for NOT TRUE we get:

00000000b = 0 = FALSE

Again, but assuming that TRUE = 1 (00000001b), for NOT TRUE we get:

11111110b = 254
caseih
Posts: 1360
Joined: Feb 26, 2007 5:32

Re: BitScanForward

Postby caseih » Nov 12, 2016 0:41

And if you do need logical operators in FB (there are times when bitwise operations fail, including when a non-False value is something other than -1), you can use AndAlso and OrElse. To my knowledge there is no logical NOT operator in FB, but bitwise NOT probably suffices if you combine it with AndAlso or OrElse. Also NOT is typically used in conjunction with a boolean comparison operator which forces the values to 0 or -1.

And regarding your example, &b11111110 is actually -2 in a signed 8-bit integer if I'm not mistaken. But yes your explanation is correct.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: BitScanForward

Postby MichaelW » Nov 12, 2016 16:18

caseih wrote:And regarding your example, &b11111110 is actually -2 in a signed 8-bit integer if I'm not mistaken.

Yes, thank you.

Return to “General”

Who is online

Users browsing this forum: No registered users and 12 guests