4
votes

Is there a tutorial somewhere that explains on which datatypes bitwise operations can be used? I don't know why Lady Ada thinks that I cannot bitwise OR two Standard.Integer...

$ gnatmake test.adb
gcc -c test.adb
test.adb:50:77: there is no applicable operator "Or" for type "Standard.Integer"
gnatmake: "test.adb" compilation error

Really? I excused the compiler for not being able to AND/OR enumerated data types. I excused the compiler for not being able to perform bitwise operations on Character type. I excused the compiler for not being able to convert from Unsigned_8 to Character in what I thought was the obvious way. But this is inexcusable.

2
You probably haven't found a bug, but you should read this nonetheless. chiark.greenend.org.uk/~sgtatham/bugs.html - Pascal Cuoq
If you see something you don't understand in a programming language, your first assumption should not be that the designers were idiots. - Keith Thompson
I was using Natural type which is a subtype of Integer, which surprisingly results in the error. Although you're putting words in my mouth by writing that I said the designers were idiots, I'm more inclined to think that the designers thought it was too complex to be able to translate the code to something like orl %eax, %ecx. I don't know why, of course. C doesn't complain. - Arjun
Implementing bitwise operators for integer would not have been difficult at all. C has the philosophy of letting the programmer do anything that might make sense. Ada's philosophy is more about catching errors as early as possible. You can do bitwise operations on integers in Ada; you just can't do it the particular way you're trying to do it. Ada is not just C with { and } replaced by begin and end; it's a different language, and you'll have much better luck with it if you try to approach it on its own terms. - Keith Thompson
You're right, as I've only just realised. This was a major pain in the ass when I was writing my own interface to OpenGL (there aren't any good ones out there that follow the Ada naming convention Like_This), and I made enumerated data types for things like GL_RGBA just like it was in C, only to find that I cannot bitwise OR GL_RGBA with other GL_*... I had to do some nonsense like GL_RGBA'Enum_Rep or some Unchecked_Conversion nonsense. Constants is the only way to go. - Arjun

2 Answers

22
votes

No one seems to want to just come out and answer the damn question:

Ada doesn't provide logical (bit-wise) operations on integer types, it provides them on modular types. Here's the section in the reference manual.

13
votes

The "and", "or", and "xor" operators are defined for Boolean, for modular types, and for one-dimensional arrays of Boolean.

The language could have defined them for signed integer types, but that would create confusion given the variety of ways that signed integers can be represented. (Most implementations use two's-complement, but there are other possibilities.)

If you insist, you could define your own overloaded "or" operator, such as:

function "or"(Left, Right: Integer) return Integer is
    type Unsigned_Integer is mod 2**Integer'Size;
begin
    return Integer(Unsigned_Integer(Left) or Unsigned_Integer(Right));
end "or";

(I've verified that this compiles, but I haven't tested it, and I'd expect it to fail for negative values.)

But if you need to perform bitwise operations, you're better off using modular types or arrays of Boolean rather than signed integers.