# Bitwise Operator

## Recommended Posts

I would like to request an engine property that would disable bitwise operations on non-integer types, producing a compile error.

I have many users whose only coding experience is in Excel, either through Excel formulas or VBA.  Both of those languages use the ^ operator as a "pow" operator, so these users will often make the mistake of using the ^ operator in AngelScript.  In almost every case, these users are using the ^ operator by mistake with at least one floating-point type or a class that can convert to a floating-point type.  A compile error that catches "(double) ^ (int)" would help these users learn that ^ is an XOR operator.

Since the engine doesn't actually support bitwise, floating-point operations (instead, implicitly converting floating-point types to integer types before the operation), it doesn't seem like an option to produce a compile error would cause much trouble.

Let me know what you think.  Thanks.

##### Share on other sites

Thanks for the suggestion. I'll certainly consider it.

##### Share on other sites

I've implemented this now.

I decided not to add any engine property though. Perhaps if someone has a need for it I'll add it, but for now the compiler will always give an error when attempting bitwise operations with float or double.

##### Share on other sites

Thanks for implementing this.  I do have one question.  I notice you are testing the left-hand and right-hand contexts for floating-point types.  Since the bit-wise comparison only applies to integral types, why not test more explicitly for those (like below)?

// Also do not permit implicit convertion to integer in this case as the user may think
// the result is a bitwise operation on the original type but it's not
if (!lctx->type.dataType.IsIntegerType() && !lctx->type.dataType.IsUnsignedType())
{
asCString str;
Error(str, node);

// Set an integer value and allow the compiler to continue
ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
return;
}
if (!rctx->type.dataType.IsIntegerType() && !rctx->type.dataType.IsUnsignedType())
{
asCString str;
Error(str, node);

// Set an integer value and allow the compiler to continue
ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
return;
}

##### Share on other sites

Only float and double are explicitly restricted here, because these types have an implicit conversion to integer but I do not want to allow them here.

Object types that implement implicit conversion to integer are still allowed.

## Create an account

Register a new account

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
13
5. 5

• 9
• 18
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632605
• Total Posts
3007371

×