# unsigned right shift

Hi , in javascript theres >>> and its called an "unsigned right shift"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift

Is >> the equivalent in mql5 ?

Thanks

Unsigned right shift (>>>) - JavaScript | MDN
• 2023.04.05
• developer.mozilla.org
Unlike other arithmetic and bitwise operators, the unsigned right shift operator does not accept BigInt values. This is because it fills the leftmost bits with zeroes, but conceptually, BigInts have an infinite number of leading sign bits, so there's no "leftmost bit" to fill with zeroes. The operator operates on the left operand's bit...

### Right Shift

The binary representation of x is shifted to the right by y digits. If the value to shift is of the unsigned type, the logical right shift is made, i.e. the freed left-side bits will be filled with zeroes.

If the value to shift is of a sign type, the arithmetic right shift is made, i.e. the freed left-side digits will be filled with the value of a sign bit (if the number is positive, the value of the sign bit is 0; if the number is negative, the value of the sign bit is 1).

`x = x >> y;`

### Left Shift

The binary representation of x is shifted to the left by y digits, the freed right-side digits are filled with zeros.

`x = x << y;`
Documentation on MQL5: Language Basics / Operations and Expressions / Bitwise Operations
• www.mql5.com
Bitwise Operations - Operations and Expressions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Fernando Carreiro #:

### Right Shift

The binary representation of x is shifted to the right by y digits. If the value to shift is of the unsigned type, the logical right shift is made, i.e. the freed left-side bits will be filled with zeroes.

If the value to shift is of a sign type, the arithmetic right shift is made, i.e. the freed left-side digits will be filled with the value of a sign bit (if the number is positive, the value of the sign bit is 0; if the number is negative, the value of the sign bit is 1).

### Left Shift

The binary representation of x is shifted to the left by y digits, the freed right-side digits are filled with zeros.

thanks.

I did not explain this properly ,sorry .I am forced to use an int or long as typescript has the type "number" (a long judging by the outcome in typescript/javascript)

also , off topic , shouldn't (-32)>>7 be zero ?

Lorentzos Roussos #: I did not explain this properly ,sorry .I am forced to use an int or long as typescript has the type "number" (a long judging by the outcome in typescript/javascript)

also , off topic , shouldn't (-32)>>7 be zero ?

It is best to shift unsigned integers, or else it will seem a little confusing.

-32 = 0xFFFFFFE0 (11111111 11111111 11111111 11100000)

shifting it 4 bits to right, makes it 268435454 = 0x0FFFFFFE (00001111 11111111 11111111 11111110)

shifting it 7 bits to right, makes it 33554431 = 0x01FFFFFF (00000001 11111111 11111111 11111111)

EDIT: I see what you mean. According to the documentation is should be filling in the left bit with the sign bit and not zero.

Show your code, as there might be a typecast into an unsigned which is causing the issue.

Lorentzos Roussos:

Hi , in javascript theres >>> and its called an "unsigned right shift"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift

Is >> the equivalent in mql5 ?

Thanks

There is no unsigned right shift in mql5. You need to be creative.

```// Not compiled, not tested.

union toShift
{
int  vsigned;
uint vnotsigned;
};

void Start()
{
toShift i;
i.vsigned = -32;
Print(i.vnotsigned>>7);
}
```

Fernando Carreiro #:

It is best to shift unsigned integers, or else it will seem a little confusing.

-32 = 0xFFFFFFE0 (11111111 11111111 11111111 11100000)

shifting it 4 bits to right, makes it 268435454 = 0x0FFFFFFE (00001111 11111111 11111111 11111110)

shifting it 7 bits to right, makes it 33554431 = 0x01FFFFFF (00000001 11111111 11111111 11111111)

EDIT: I see what you mean. According to the documentation is should be filling in the left bit with the sign bit and not zero.

Show your code, as there might be a typecast into an unsigned which is causing the issue.

it does it for the unsigned types but not for the signed i think . Thanks

```   int a=-32;
for(int i=0;i<8;i++){
int sh=a>>i;
Print(IntegerToString(a)+" >> "+IntegerToString(i)+" = "+IntegerToString(sh));
} ```

Alain Verleyen #:
id Start() {     toShift i;     i.vsigned = -32;     Print(i.vnotsigned>>7); }

thanks will test it

----

it almost did it

```  // Not compiled, not tested.

union toShift
{
int  vsigned;
uint vnotsigned;
};

toShift x;
x.vsigned=-32;
for(int i=0;i<8;i++){
int sh=x.vnotsigned>>i;
Print(IntegerToString(x.vsigned)+" >> "+IntegerToString(i)+" = "+IntegerToString(sh));
} ```

Lorentzos Roussos #:

also , off topic , shouldn't (-32)>>7 be zero ?

No. It can never be 0 with a negative number, as 1 are filled on the left side, so the "end" of the shift is always -1.

Lorentzos Roussos #:

thanks will test it

----

it almost did it

Yeah, for sure the >>0 is a bit special. You will certainly find how to solve it.

Alain Verleyen #: There is no unsigned right shift in mql5. You need to be creative.
Huh? ... " If the value to shift is of the unsigned type, the logical right shift is made, "

Alain Verleyen #:
Yeah, for sure the >>0 is a bit special. You will certainly find how to solve it.

will do