Ada中的按位操作

Arj*_*jun 2 bit-manipulation ada

是否有某个教程可以解释哪些数据类型可以使用按位运算?我不知道为什么阿达夫人认为我不能按比例或两个标准.整个......

$ 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
Run Code Online (Sandbox Code Playgroud)

真?我原谅编译器无法进行AND/OR枚举数据类型.我原谅编译器无法对字符类型执行按位操作.我原谅编译器无法从Unsigned_8转换为Character,这是我认为显而易见的方式.但这是不可原谅的.

Mar*_*c C 16

似乎没有人想出来回答该死的问题:

Ada不对整数类型提供逻辑(逐位)操作,它在模块类型上提供它们.这是参考手册中部分.


Kei*_*son 8

"and","or""xor"运营商都为限定Boolean,模块化类型,而对于一维数组Boolean.

该语言可以为已签名的整数类型定义它们,但这会产生混淆,因为可以表示有符号整数的各种方式.(大多数实现使用二进制补码,但还有其他可能性.)

如果你坚持,你可以定义自己的重载"or"运算符,例如:

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";
Run Code Online (Sandbox Code Playgroud)

(我已经验证了这个编译,但我没有对它进行测试,我希望它会因为负值而失败.)

但是如果你需要执行按位运算,最好使用模块类型或数组Boolean而不是有符号整数.