xiaolingzinia 于 2016.02.26 14:52 提问

java紧急求助，谢谢各位 1C

package com.houlibao.www;

public class TestExample {

``````public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 7;/* 00000111 */
n <<= 3;
System.out.println(n); /* 111000 */
System.out.println(Integer.toBinaryString(n&n));/* 111000 */
System.out.println(Integer.toBinaryString(n|1));/* 111001 */
System.out.println(Integer.toBinaryString(2^n));/* 111010 */

//  n = n&n + n|1 + 2^n ; /* 59 111011 */
n = n&n + n|1 + 2^n +3 ;  /* 56 111000 */
System.out.println(n);  /* 56 111000 */

}
``````

}

9个回答

wojiushiwo945you      2016.02.26 21:11

`````` n = n & (n + n) | (1 + 2 ^ n + 3);
``````

wojiushiwo945you      2016.02.26 21:14

`````` n = n & (n + n) | ((1 + 2) ^( n + 3));
``````
u012427018   2016.02.26 16:10

u012427018   2016.02.26 16:17

Royal_lr      2016.02.26 16:39

xionglangs   2016.02.26 17:19

，我运行的2个结果都是56。<< 和<<= 都是位移运算符，但<<是进行位移运算，<<=是进行位移运算后把值赋给原来的值，比如这里的n
7<<3也就是二进制运算00000000 000000000000000000000111 7=2^（3-1）+2^（2-1）+2^（1-1），0就不用算右移3位变成了00000000000000000000000000111000，运算得56，再把56赋值给n，所以打印出来的就是56。

System.out.println(Integer.toBinaryString(00000000000000000000000000111000&00000000000000000000000000111000));/* 111000 /

System.out.println(Integer.toBinaryString(00000000000000000000000000111000|00000000000000000000000000000001));/
111001 /

System.out.println(Integer.toBinaryString(00000000000000000000000000000010^00000000000000000000000000111000));/
111010 */

n = n&n + n|1 + 2^n +3

Mr_dsw      2016.02.26 17:26

shanxianyue1   2016.02.27 18:16

shallynever   2016.02.27 23:48