Jak działa bitshifting w Javie?

Mam takie stwierdzenie:

Przyjmij wartość bitową bajtu x to 00101011. jaki jest wynik x>>2?

Jak mogę go zaprogramować i czy ktoś może mi wyjaśnić co robi?

Author: Jeroen Vannevel, 2010-07-22

10 answers

Po pierwsze, możesz Nie przesunąć a byte w Javie, możesz tylko przesunąć an int lub a long. Więc byte najpierw przejdzie awans, np.

00101011 -> 00000000000000000000000000101011

Lub

11010100 -> 11111111111111111111111111010100

Teraz, x >> N oznacza (jeśli widzisz go jako ciąg cyfr binarnych):

  • prawe N bitów są odrzucane
  • lewy bit jest replikowany tyle razy, ile jest to konieczne, aby uzyskać wynik do oryginalnego rozmiaru (32 lub 64 bity), np.

00000000000000000000000000101011 >> 2 -> 00000000000000000000000000001010

11111111111111111111111111010100 >> 2 -> 11111111111111111111111111110101

 89
Author: finnw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-22 20:01:02

Operatorzy Zmian

Binarne 32 bity dla 00101011 to

00000000 00000000 00000000 00101011, a wynikiem jest:

  00000000 00000000 00000000 00101011   >> 2(times)
 \\                                 \\
  00000000 00000000 00000000 00001010

Przesuwa bity 43 w lewo o odległość 2; wypełnia najwyższym (znakowym) bitem po lewej stronie.

Wynik to 00001010 z wartością dziesiętną 10.

00001010
    8+2 = 10
 52
Author: Chandra Sekhar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-04 07:57:54

Kiedy przesuniesz w prawo 2 bity, upuścisz 2 najmniej znaczące bity. Więc:

x = 00101011

x >> 2

// now (notice the 2 new 0's on the left of the byte)
x = 00001010

Jest to zasadniczo to samo, co dzielenie int przez 2, 2 razy.

W Javie

byte b = (byte) 16;
b = b >> 2;
// prints 4
System.out.println(b);
 15
Author: jjnguy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-22 20:14:57

Przykłady te obejmują trzy rodzaje przesunięć stosowanych zarówno do liczby dodatniej, jak i ujemnej:

// Signed left shift on 626348975
00100101010101010101001110101111 is   626348975
01001010101010101010011101011110 is  1252697950 after << 1
10010101010101010100111010111100 is -1789571396 after << 2
00101010101010101001110101111000 is   715824504 after << 3

// Signed left shift on -552270512
11011111000101010000010101010000 is  -552270512
10111110001010100000101010100000 is -1104541024 after << 1
01111100010101000001010101000000 is  2085885248 after << 2
11111000101010000010101010000000 is  -123196800 after << 3


// Signed right shift on 626348975
00100101010101010101001110101111 is   626348975
00010010101010101010100111010111 is   313174487 after >> 1
00001001010101010101010011101011 is   156587243 after >> 2
00000100101010101010101001110101 is    78293621 after >> 3

// Signed right shift on -552270512
11011111000101010000010101010000 is  -552270512
11101111100010101000001010101000 is  -276135256 after >> 1
11110111110001010100000101010100 is  -138067628 after >> 2
11111011111000101010000010101010 is   -69033814 after >> 3


// Unsigned right shift on 626348975
00100101010101010101001110101111 is   626348975
00010010101010101010100111010111 is   313174487 after >>> 1
00001001010101010101010011101011 is   156587243 after >>> 2
00000100101010101010101001110101 is    78293621 after >>> 3

// Unsigned right shift on -552270512
11011111000101010000010101010000 is  -552270512
01101111100010101000001010101000 is  1871348392 after >>> 1
00110111110001010100000101010100 is   935674196 after >>> 2
00011011111000101010000010101010 is   467837098 after >>> 3
 7
Author: Percy Vega,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-24 04:37:59

>> jest arytmetycznym prawym operatorem przesunięcia. Wszystkie bity pierwszego operandu są przesunięte o liczbę miejsc wskazywanych przez drugi operand. Najbardziej lewe bity w wyniku są ustawione na tę samą wartość, co najbardziej lewe bity w liczbie oryginalnej. (Jest tak, że liczby ujemne pozostają ujemne.)

Oto twój konkretny przypadek:

00101011
  001010 <-- Shifted twice to the right (rightmost bits dropped)
00001010 <-- Leftmost bits filled with 0s (to match leftmost bit in original number)
 5
Author: Mike Daniels,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-22 19:59:52
public class Shift {
 public static void main(String[] args) {
  Byte b = Byte.parseByte("00101011",2);
  System.out.println(b);
  byte val = b.byteValue();
  Byte shifted = new Byte((byte) (val >> 2));
  System.out.println(shifted);

  // often overloked  are the methods of Integer

  int i = Integer.parseInt("00101011",2);
  System.out.println( Integer.toBinaryString(i));
  i >>= 2;
  System.out.println( Integer.toBinaryString(i));
 }
}

Wyjście:

43
10
101011
1010
 4
Author: stacker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-22 20:27:29
byte x = 51; //00101011
byte y = (byte) (x >> 2); //00001010 aka Base(10) 10
 2
Author: Jacob Tomaw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-22 20:04:10

Nie możesz pisać liter binarnych, takich jak 00101011 w Javie, więc możesz zapisać je w systemie szesnastkowym:

byte x = 0x2b;

Aby obliczyć wynik x >> 2, możesz po prostu napisać dokładnie to i wydrukować wynik.

System.out.println(x >> 2);
 2
Author: Mark Byers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-22 20:05:50

Możesz użyć np. tego API, jeśli chcesz zobaczyć prezentację bitString swoich liczb. Matematyka

Przykład (w jruby)

bitString = org.uncommons.maths.binary.BitString.new(java.math.BigInteger.new("12").toString(2))
bitString.setBit(1, true)
bitString.toNumber => 14

Edit : zmieniono link do api i dodano mały przykład

 2
Author: Joni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-06 19:47:07

00101011 = 43 w układzie dziesiętnym

class test {    
    public static void main(String[] args){
       int a= 43;       
       String b= Integer.toBinaryString(a >> 2);        
       System.out.println(b);
    }   
}

Wyjście:

101011 staje się 1010

 0
Author: Gihan Fernando,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-06 19:30:36