Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is >>> 0 in javascript?

I am using a complied Emscripten port of LibTIFF C code called tiff.js by seikichi on github. In my code I need to get some of the TIFF Tags. In tiff.js you can call tiff.getField(tag value). One tag I need specifically is ROWSPERSTRIP with is 278 so to get this tag I call as follows:

var rps = tiff.getField(278); //return rows per strip tiff tag

This seems to work fine for some smaller values 1 to 176 (not exactly sure yet?) but I have several files that AsTiffTagViewer reports as 224 rows per strip and one that file with 746 rows per strip. However tiff.js gets reports both of this values incorrectly as 6 and 1 respectively. I was stepping through the debugger in tiff.js and notice it comes to a place with the following code:

do{if(b>>>0<=65535){d=e[j+24>>1]|0;if((1<<(d&31)&c[a+40+(d>>>5<<2)>>2)]|0)==0){k=0}else{break}i=f;return k|0}}while(0);

It's ugly I suspect because its the best Emscripten can do with C code. Now I know what ">>" and "<<" do but I can't seem to find anything about ">>>". I don't have any idea yet if this is the section of code in tiff.js that is causing tiff.getField(278) to not return some larger rows per strip values incorrectly (all I know right now is it appears smaller rows per strip values are returned correctly where as larger ones are not returned correctly it appears.

So my main question is what is ">>>" and my second question is does anyone have any ideas on why tiff.getField(278) may not be working correctly for larger values. NOTE: most of the other basic TIFF Tags do return correct values such as tiff.getField(PHOTOMETRIC) and tiff.getField(SAMPLESPERPIXLE)...etc.

Thanks in advance for ideas

like image 270
Neal Davis Avatar asked Sep 21 '26 03:09

Neal Davis


2 Answers

It's a bitwise zero-fill right shift, from MDN

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes 0, so the result is always non-negative.

For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example, 9 >>> 2 yields 2, the same as 9 >> 2:

As for why tiff.js doesn't work when you call tiff.getField(278), I have no idea, I think it should, and there doesn't seem to be any known issues about this on the Github pages, but you can try adding an issue and see if the developers know.

like image 163
adeneo Avatar answered Sep 22 '26 17:09

adeneo


-3 >>> 0 is an unsigned shift right by 0 bits. I.e. the number is converted to an unsigned 32bit integer as opposed to -3 | 0 which converts the number to a 32bit signed integer.

-3 >>> 0 === 4294967293
-3 | 0 === -3

11.7.3 The Unsigned Right Shift Operator ( >>> )

Performs a zero-filling bitwise right shift operation on the left operand by the amount specified by the right operand.

The production ShiftExpression : ShiftExpression >>> AdditiveExpression is evaluated as follows:

  1. Let lref be the result of evaluating ShiftExpression.

  2. Let lval be GetValue(lref).

  3. Let rref be the result of evaluating AdditiveExpression.

  4. Let rval be GetValue(rref).

  5. Let lnum be ToUint32(lval).

  6. Let rnum be ToUint32(rval).

  7. Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.

  8. Return the result of performing a zero-filling right shift of lnum by shiftCount bits. Vacated bits are filled with zero. The result is an unsigned 32-bit integer.

like image 39
kay Avatar answered Sep 22 '26 16:09

kay