2

I'm making an application in C#, based on Delphi (converting code), but I found a command that I do not recognize (shl), and i want to know if there is any equivalent to C#.

Thanks in advance.

Daas Cook
  • 377
  • 1
  • 6
  • 10

3 Answers3

7

Shl is left shift. Use << C# operator for the same effect.

Example:

uint value = 15;              // 00001111
uint doubled = value << 1;    // Result = 00011110 = 30
uint shiftFour = value << 4;  // Result = 11110000 = 240
Paya
  • 5,124
  • 4
  • 45
  • 71
  • 2
    Fun fact: `x << y` is also equivalent to `x * 2 to the power of y`. Likewise, `x >> y` divides by 2 y times – Rei Miyasaka Mar 18 '11 at 05:55
  • 7
    Another fun fact, C/C#'s `>>` operator is an arithmetic shift, that is, it shifts in 0s from the left on unsigned types but shifts in 1s on signed types, thus it always divides by 2. Delphi's `shr` function is a logical shift and always shifts-in 0s. (That was a comparison of C#4 & Delphi 7 btw - I can't find a formal definition of Delphi's `shr`). – MikeJ-UK Mar 18 '11 at 09:25
2

From Shl Keyword this is a bitwise shift left which from C# Bitwise Shift Operators would be <<

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
1

Shl is the shift left operator, in C# you use <<.

var
  a : Word;
begin
  a := $2F;   // $2F = 47 decimal
  a := a Shl $24;
end; 

is the same as:

short a = 0x2F;
a = a << 0x24;
dalle
  • 18,057
  • 5
  • 57
  • 81
  • 5
    They're the same, but only by accident. The Delphi code will truncate the $24 value to the lower 4 bits because `a` is a 16-bit type. The C# code will promote `a` to type `int`, which is a 32-bit type, and then truncate 0x24 to the lower *5* bits. In this case, that happens to be equal to the lower 4 bits, but shift by an amount that uses the fifth bit and you should see a difference. – Rob Kennedy Mar 18 '11 at 07:10