Can Someone Clarify How Xojo Handles Bitwise Overrides?

There’s been a recurrent theme in my programming endeavours over the last week - binary maths.

Having overcome an enormous hurdle thanks to people like @npalardy yesterday, I wanted to ask for some clarification about another binary maths issue.

I understand the concept of ANDing and ORing one integer against another (bit-masking, flipping bits, etc) but I just wanted some clarification about how Xojo handles bitwise operations between integers of different sizes.

I know that if I AND / OR against a numeric literal, the Xojo compiler will infer that the numeric literal is an Int64 (at least on 64-bit platforms). What is the effect of performing a bitwise operation between a 32-bit integer and a 64-bit integer, for instance?

For example:

Var i64 As Int64 = 100
Var i32 As Int32 = 100
Var mask As Int32 = &hFF00000000000000

Var result64 As Int64 = i64 Or mask // = 100
Var result32 As Int64 = i32 Or mask // = 100

Obviously the results here are the same but I’m trying to establish if there are scenarios where this isn’t true.

Not sure I understand what you’re asking.

This would be handled in much the same way assigning a 32 bit int to a 64 bit int is.
Its a process known as “sign extension”
Basically you take the sign bit and replicate it
This means you do not alter the VALUE when represented with more bits
ie/ if we start with an 8 bit value like 10 (in binary &b00001010)
to turn this into a 16 bit value and NOT change the fact that the new 16 bit value should still hold 10 - just in 16 bits we replicate the sign bit (literally the one in &b notation right next to the &b) into the new 8 bits
so our new value is &b00000000 00001010 which is still 10

the same is true in a negative value
-1 is &b11111111
to make this 16 bit we replicate the sign bit and get
&b11111111 11111111

This is just how assigning a value in a smaller integer size to a larger one works

AND OR NOT etc will, as far as I know, all promote their operands to a common type & size, do the operation and then assign that to the resulting sized type

This type promotion happens for all binary operators as far as I recall
So an Int 8 * Int32 will promote the int8 to an int32 do the * and then assign the resulting int32 to whatever variable gets the result

1 Like

Thanks @npalardy, that’s exactly what I was asking and was what I thought would happen. That’s saved me having to recheck about 5000 lines of code!!