October 30, 2025, 12:03:34 AM

News:

IWBasic runs in Windows 11!


Power operator and bitwise AND question

Started by Guilect, April 23, 2009, 07:13:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Guilect

The commented out line throws the error : "illegal operand"
I would like to do just that (power and bitwise AND) in a program I am writing.
Am I doing something incorrectly?

OPENCONSOLE
def temp as INT
temp = 2

PRINT 125 & 2 * temp
PRINT 125 & 2 / temp
PRINT 125 & 2 + temp
PRINT 125 & 2 - temp
'PRINT 125 & 2 ^ temp

PRINT
PRINT "Press Any Key To Close"
DO
UNTIL INKEY$ <> ""
CLOSECONSOLE
END

Ionic Wind Support Team

See the users guide about operator precedence,  use parenthesis to group.

Correct: PRINT (125 & 2) ^ temp

The & operator only works with integer types, as it would meaningless for decimal values. ^ has higher precedence than & so the compiler is trying to do this:

PRINT 125 & (2 ^ temp)

The result of a ^ operator is a DOUBLE precision number, and then you get the error from &

Paul.
Ionic Wind Support Team

Guilect

Paul,
great explaination.  (yes, I know, RTM (read the manual)).   ;)

It turns out, that I am in fact trying to do the : PRINT 125 & (2 ^ temp)
So, I'll use this : PRINT 125 & INT(2 ^ temp)
The INT(double) returns an INT64; I can work with that.

Thanks.