3

im trying to translate this code to python, but im having a hard time doing so, don't worry about the index values and variable names, I just want to know what the ">>>" part does exactly, perhaps a python equivalent, but an explanation would be great:

target[0] = (char)(source[sourceIndex] >>> 2);
target[1] = (char)((source[sourceIndex] & 3) << 4 | source[sourceIndex + 1] >>> 4);
target[2] = (char)((source[sourceIndex + 1] & 0xf) << 2 | source[sourceIndex + 2] >>> 6);
target[3] = (char)(source[sourceIndex + 2] & 0x3f);

Any help would be appreciated

Ole
  • 1,363
  • 12
  • 22
  • 2
    Its difficult to search for '>>>' on Google. – theomega Feb 25 '12 at 13:07
  • 2
    @Eels: That's a bit unfair, since it might not be obvious that the right search is "Java operators" rather than something like "java >>>". – Russell Zahniser Feb 25 '12 at 13:08
  • 2
    I had no idea what this was called so googling for "java >>>" didnt return anything useful at all. – Ole Feb 25 '12 at 13:08
  • 2
    possible duplicate of [Difference between >>> and >>](http://stackoverflow.com/questions/2811319/difference-between-and) – aioobe Feb 25 '12 at 13:09

3 Answers3

10

It's an "unsigned right shift".

So, if your number (x) is 11110000 (in binary).

x >>> 1 will be 01111000 (in binary).

This is opposed to x >> 1 which will result in 11111000 (in binary).

The >> tries to preserve the "sign bit" but the >>> does not.

Note: I've assumed a 8-bit integer (or a byte in Java). The same thing holds for 2-byte and 4-byte integers.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Chip
  • 3,226
  • 23
  • 31
8

The "<<<" and ">>" are bit shift operators. Specifically,

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.

—— from The Java™ Tutorials - Bitwise and Bit Shift Operators

Pang
  • 9,564
  • 146
  • 81
  • 122
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
2

Thats the unsigned right shift operator. It's a bitwise operator that shifts a zero into the leftmost bit of your operand. Here - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html.

Perception
  • 79,279
  • 19
  • 185
  • 195