Вопрос по java – Как работают операторы сдвига в Java? [Дубликат]

127

This question already has an answer here:

Я пытаюсь понять операторы сдвига и не могу получить много. Когда я попытался выполнить приведенный ниже код

System.out.println(Integer.toBinaryString(2 << 11));
System.out.println(Integer.toBinaryString(2 << 22));
System.out.println(Integer.toBinaryString(2 << 33));
System.out.println(Integer.toBinaryString(2 << 44));
System.out.println(Integer.toBinaryString(2 << 55));

Error: User Rate Limit Exceeded

1000000000000    
100000000000000000000000    
100    
10000000000000    
1000000000000000000000000    

Может кто-нибудь объяснить, пожалуйста?

Error: User Rate Limit Exceeded m0skit0

Ваш Ответ

9   ответов
7

Signed left shift

22 *(2^11) = 4096

Signed right shift

40964096 / (2^11) = 2

2

0's.

binary 10 which is digit 2 left shift by 2 is 1000 which is digit 8 binary 10 which is digit 2 left shift by 3 is 10000 which is digit 16
1

Signed left shiftError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

// it will result in 4096

2<<11 = 2*(2^11) = 4096
12

    System.out.println(Integer.toBinaryString(2 << 0));
    System.out.println(Integer.toBinaryString(2 << 1));
    System.out.println(Integer.toBinaryString(2 << 2));
    System.out.println(Integer.toBinaryString(2 << 3));
    System.out.println(Integer.toBinaryString(2 << 4));
    System.out.println(Integer.toBinaryString(2 << 5));

Result

    10
    100
    1000
    10000
    100000
    1000000

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
33

Error: User Rate Limit Exceeded

10

Error: User Rate Limit Exceeded

2 << 11

Error: User Rate Limit Exceeded

1000000000000

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension [..]

Error: User Rate Limit Exceeded

For example

Error: User Rate Limit Exceeded10Error: User Rate Limit Exceeded<<1Error: User Rate Limit Exceeded100Error: User Rate Limit Exceeded4

Error: User Rate Limit Exceeded100Error: User Rate Limit Exceeded<<1Error: User Rate Limit Exceeded1000Error: User Rate Limit Exceeded8

Also See

absolute-beginners-guide-to-bit-shifting
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededbinary formError: User Rate Limit Exceeded
<!--->Error: User Rate Limit Exceeded
2

value= value >> steps  // Right shift, signed data.
value= value << steps  // Left shift, signed data.
14

Error: User Rate Limit Exceeded

value >> num

Error: User Rate Limit Exceeded

int a = 32;
a = a >> 2; // a now contains 8

Error: User Rate Limit Exceeded

int a = 35;
a = a >> 2; // a still contains 8

Error: User Rate Limit Exceeded

00100011 35 >> 2
00001000 8

Error: User Rate Limit Exceeded–8 >> 1Error: User Rate Limit Exceeded–4Error: User Rate Limit Exceeded

11111000 –8 >>1
11111100 –4

Error: User Rate Limit Exceeded

// Masking sign extension.
class HexByte {
  static public void main(String args[]) {
    char hex[] = {
      '0', '1', '2', '3', '4', '5', '6', '7',
      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
    };
  byte b = (byte) 0xf1;
 System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}

Error: User Rate Limit Exceeded

b = 0xf1
194
System.out.println(Integer.toBinaryString(2 << 11)); 

101000000000000

System.out.println(Integer.toBinaryString(2 << 22)); 

10100000000000000000000000

System.out.println(Integer.toBinaryString(2 << 33)); 

100

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededcount >= type widthError: User Rate Limit Exceededstackoverflow.com/questions/776508/…Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededMath.Pow()Error: User Rate Limit ExceededdoubleError: User Rate Limit ExceededintError: User Rate Limit ExceededdoubleError: User Rate Limit ExceededMath.pow()Error: User Rate Limit ExceededintError: User Rate Limit ExceededintError: User Rate Limit Exceeded
0

<<=, >>=>>>=.

i >>= 2

i = i >> 2

Похожие вопросы