在Java中 ,对于正数,它的原码,反码,补码都相同,
对于负数, 如 -5 它的 原码为 1 000 0000 .......0000 0101
反码为(符号位不变,其他的位求反加1) 1 111 1111 .... 1111 1010
补码为 反码加1 1 111 1111 .... 1111 1011
例如: int a=+5;
由于在Java中 int 都是默认的 32 位有符号二进制表示的,
因此+5的二进制表示为 0 000 0000 .......0000 0101
第一个0 表示符号位, 0 表示正 1 表示负
对于 -5 他在Java中的二进制表示为 1 000 0000 0000 .......0101 ,
在进行运算时,Java中都是用补码进行运算的, 对于 +5 它的补码和原码相同
如果进行一次取反运算 ~5
取反运算的过程如下 先求得补码 为 0 000 0000 .......0000 0101
再取反,这里注意 (取反运算和求一个数的反码是有区别的, 取反运算包括对符号位的操作,而求一个数的反码并不包括对符号位的操作) 的到 1 111 1111 1111 .......1111 1010
此时我们便得到了 正5 取反后的结果 因为我们是对+5 的补码进行操作的,此时的到的应该是 结果 的补码,
再将补码转换为原码 (除符号位外,将其他位求反加1)得到: 1 000 0000 0000 ....0000 0110 即 为 -6
基本的逻辑运算
Java提供了一组运算符丰富的操纵变量。我们可以把所有的Java操作符为以下几组:
-
算术运算符
-
关系运算符
-
位运算符
-
逻辑运算符
-
赋值运算符
-
其它运算符
算术运算符:
算术运算符用于在数学表达式中,他们是在代数中使用的方法相同。下表列出了算术运算符:
假设整型变量A=10和变量B=20,则:
运算符 | 描述 | 实例 |
---|---|---|
+ | Addition - Adds values on either side of the operator | A + B = 30 |
- | Subtraction - Subtracts right hand operand from left hand operand | A - B = -10 |
* | Multiplication - Multiplies values on either side of the operator | A * B = 200 |
/ | Division - Divides left hand operand by right hand operand | B / A = 2 |
% | Modulus - Divides left hand operand by right hand operand and returns remainder | B % A = 0 |
++ | Increment - Increases the value of operand by 1 | B++ =21 |
-- | Decrement - Decreases the value of operand by 1 | B-- =19 |
关系运算符:
有下列由Java语言支持的关系运算符
假设变量A=10和变量B=20,则:
运算符 | 描述 | 实例 |
---|---|---|
== | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
按位运算符:
Java定义了几个位运算符,它可以应用到整数类型,长型,整型,短整型,字符和字节。
位运算符作用于位,并执行逐位操作。假设当a =60和b= 13; 现在以二进制格式,他们将会如下:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
下表列出了按位运算符:
假设整型变量A=60和变量B=13,则:
运算符 | 描述 | 实例 |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) will give 12 which is 0000 1100 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) will give 61 which is 0011 1101 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) will give 49 which is 0011 0001 |
~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 will give 240 which is 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 will give 15 which is 1111 |
>>> | Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. | A >>>2 will give 15 which is 0000 1111 |
逻辑运算符:
下表列出了逻辑运算符:
假设布尔变量A=ture,变量B=false,那么:
运算符 | 描述 | 实例 |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is true. |