6System.out.println("The result of the 3^5 operation is:"+ (3^5));2. And:& If both input bits are 1, the result after the "&" operation is 1, otherwise the result is 0//3:0011//5:0101//&"/> 6System.out.println("The result of the 3^5 operation is:"+ (3^5));2. And:& If both input bits are 1, the result after the "&" operation is 1, otherwise the result is 0//3:0011//5:0101//&">

Home >Java >javaTutorial >What bit operations are there in java and how to use them

What bit operations are there in java and how to use them

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2023-04-30 21:46:131429browse

1. XOR: ^

If one of the two input bits is 1 and the other is not 1, then the result after the "^" operation is 1

// 3: 0011
// 5: 0101
// ^的规则是:若两个输入位的某一个是 1,另一个不是 1,那么 "^" 运算后结果才是 1
// ---0110->6
System.out.println("3^5运算的结果是 :" + (3 ^ 5));

2, and:&

If both input bits are 1, the result after the "&" operation is 1, otherwise the result is 0

// 3: 0011
// 5: 0101
// &的规则是:若两个输入位都是 1,则"&" 运算后结果是 1,否则结果是 0
// ---0001->1
System.out.println("3&5运算的结果是 :" + (3 & 5));

or:|

If at least one of the two input bits is 1, then the result of the "|" operation is 1. If both are 0, the result is 0

// 3: 0011
// 5: 0101
// |的规则是:若两个输入位里至少有一个是 1,则"|" 运算后结果是 1,都是0的情况下结果是 0
// ---0111->7
System.out.println("3|5运算的结果是 :" + (3 | 5));

NOT:~

The logic of NOT is relatively simple, 0->1,1->0

//  3: 0011
// ~3: 1100-> -4
// 其中,第一位表示正负值
System.out.println("~3运算的结果是:" + ~3);

The above is the detailed content of What bit operations are there in java and how to use them. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete