<分区>
看看下面的代码:
案例 1:
char a = 'x' ^ 'y';
它工作正常。 但是当我在这里使用变量而不是常量时:
案例 2:
char x = 'x';
char y = 'y';
char a = x ^ y; // Error!
In java language : case 1 is working and value of a comes out to be 1 but case 2 is not working.
In C and C++ : both cases are working, and the value of a comes out to be 1
In C# : both the cases are not working.
In javascript : both cases are working, which is not a HLL, and value of a comes out to be 0.
我知道 java 正在将变量转换为整数以进行二进制操作,但为什么它在情况 1 中有效而不在情况 2 中有效,为什么在 C# 中不起作用以及为什么值在 javascript 中不同。
已更新 当我将变量设置为 final 时,它可以在 Java 中使用,但在 C# 中仍然不行
final char x = 'x';
final char y = 'y';
char a = x ^ y;
但我仍然无法理解为什么常量有效,但在使用变量时却无效。为什么其他高级编程语言没有发生同样的事情。
我认为这是一个基本操作,应该适用于具有相同行为的所有编程语言。
Note To test all above cases in javascript I am replacing 'char' with 'var' in all cases and they are working.