9

Generally speaking, bit shifting (>> , <<) allows us to divide / multiply by ^2

Example :

      9 (base 10): 00000000000000000000000000001001 (base 2)
                   --------------------------------
 9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)

For negative numbers :

Likewise, -9 >> 2 yields -3, because the sign is preserved:

     -9 (base 10): 11111111111111111111111111110111 (base 2)
                   --------------------------------
-9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)

But looking at >>> which acts the same for positive numbers, but behaves differently for negative numbers :

mdn

Zero bits are shifted in from the left

I can't find any reason / usage for shifting 0 from the left ( which makes the whole number positive) from the left :

       -9 (base 10): 11111111111111111111111111110111 (base 2)
                     --------------------------------
 -9 >>> 2 (base 10): 00111111111111111111111111111101 (base 2) = 1073741821 (base 10)

Question :

In what scenarios should I use >>> ? I don't understand why should I ever want to pad zeros from the left and mess my negative number.

Yonggoo Noh
  • 1,811
  • 3
  • 22
  • 37
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    Read [logical right shift, the `>>>` operator](http://stackoverflow.com/questions/15457893/java-right-shift-on-negative-number/15457908#15457908), `>>` preserves sign but `>>>` not – Grijesh Chauhan Sep 29 '13 at 12:14
  • @GrijeshChauhan it's really helpful but it only explains **how** it works which I already know that. but in what scenarios I should use it ? I dont see any math operation with shifting those bits to the right and add left padding zeros. I mean , If I told you to divide a number by using bitwise operations - you would choose `>>` but when would you choose `>>>` ? – Royi Namir Sep 29 '13 at 12:16
  • ok I read your question again. Somewhere I read that unsigned shift are frequently uses in graphics programming. I don't know how. so +. – Grijesh Chauhan Sep 29 '13 at 12:17
  • Read: [Read usage of `>>>`](http://stackoverflow.com/a/16763939/1673391) sometime we use in programming to for example I used here in [my answer](http://stackoverflow.com/questions/15917454/set-the-m-bit-to-n-bit?lq=1) – Grijesh Chauhan Sep 29 '13 at 12:20
  • 1
    @RoyiNamir it depends on how you read(interpret) `11111111111111111111111111110111`. I may want to use as *unsigned int* `0xfffffff7` and do the right shift to *divide* it. – L.B Sep 29 '13 at 12:21
  • `java.util.HashMap` uses it for an internal hashing function (Javascript version: [jsava.util.HashMap@github](https://github.com/Airblader/jsava/blob/master/src/jsava/util/HashMap.js#L250), original source: [java.util.HashMap@grepcode](http://grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java/?v=source)) – Ingo Bürk Sep 29 '13 at 12:43
  • @IngoBürk changing the left most bit to 0 doesnt make it unsigned. ( with the same oposite value) – Royi Namir Sep 29 '13 at 14:00
  • Again a question like this? You just need it when you need it. On signed numbers, no, it doesn't make much sense. It makes sense if the number was supposed to be interpreted as a string of bits or as an unsigned int. – harold Sep 29 '13 at 14:31
  • @harold _interpreted as a string of bits_ can you please supply an example as an answer ? – Royi Namir Sep 29 '13 at 14:33
  • Well say a tiny bitset. I'm not sure why you'd want to right-shift that, but it's an example of interpreting a number not as the number but just as a bunch of bits. – harold Sep 29 '13 at 14:46

2 Answers2

6

A simple and often use case is to convert a variable to 32-bit unsigned integer(UInt32). When you do variable >>> 0, the variable will stay the same if it is already a UInt32 and will be 0 if it is not. e.g.

enter image description here

using example:

function convert( arrayLikeVariable){
   // we dont know for sure if length is UInt32
   // e.g. arrayLikeVariable.length = "zavarakatranemia"
   // so we do >>> 0
   var len = arrayLikeVariable >>> 0 
   // Now len is UInt32 for sure. 
   [..]
   // code using the len
}

If you want a complete example see the Polyfill here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

if (!Array.prototype.indexOf)  Array.prototype.indexOf = (function(Object, max, min){
  "use strict";
  return function indexOf(member, fromIndex) {
    if(this===null||this===undefined)throw TypeError("Array.prototype.indexOf called on null or undefined");

    var that = Object(this), Len = that.length >>> 0, i = min(fromIndex | 0, Len);
    if (i < 0) i = max(0, Len+i); else if (i >= Len) return -1;

    if(member===void 0){ for(; i !== Len; ++i) if(that[i]===void 0 && i in that) return i; // undefined
    }else if(member !== member){   for(; i !== Len; ++i) if(that[i] !== that[i]) return i; // NaN
    }else                           for(; i !== Len; ++i) if(that[i] === member) return i; // all else

    return -1; // if the value was not found, then return -1
  };
})(Object, Math.max, Math.min);
Thomas Karachristos
  • 3,237
  • 18
  • 22
5

Let's say you were programming something to mimic a piece of hardware, specifically a shift register.

To make things easier I'll use 8 bits instead of 32 bits as in your question.

We can add 128 every time we want to feed a high bit into this shift-register, since it will make the leftmost bit 1.

// Assume n is initialised to 0, so n = 00000000 in binary
n += 128;                    // now n = 10000000 in binary

If we shift using >>> every time you want to simulate a clock cycle, then after 8 "clock cycles" we will have that 1 at the rightmost bit. If we read that rightmost bit out then we will get a delayed version of what was fed into the leftmost bit 8 cycles ago.


This is only one example where the bits are not interpreted as a number, and I am sure there are many more. I think you'll find a few more uses elsewhere, especially in software meant to mimic hardware circuits/building blocks.

DevGoldm
  • 828
  • 12
  • 28