When is the shift operator >> or << useful?

Possible duplicate:
When to use Shift <→ operators in C #?

I have programming time and I have never used a shift operator. I could see how this could be useful for computing hash codes, for example in Tuple<T>, but other than that,

When and how the shift operator is useful in C # /. NET?

+3
source share
7 answers

It is useful to write the credentials of two.

Fast: what is 2 27 ?
Answer:1 << 27

Recording is 1 << 27simpler and more understandable than 134217728.

+9
source

. . ,

public static string GetBits(int value) {
  var builder = new StringBuilder();
  for (int i = 0; i < 32; i++) {
    var test = 1 << (31 - i);    
    var isSet = 0 != (test & value);
    builder.Append(isSet ? '1' : '0');
  }
  return builder.ToString();
}
+13

. , , , # , C/++, .

1:

, . ? , , htonl() ( ). - - :

        ((source & 0x000000ff) << 24 ) |
        ((source & 0x0000ff00) << 8) |
        ((source & 0x00ff0000) >> 8) |
        ((source & 0xff000000) >> 24);

2:

DMA, 512K. ( , ) 4 18- 18- DMA. , DMA. :

dma_flags | ((length & 0xffffc) << 14);

, . , , .

+3

* .NET:)

Sudoku - ?

, , () , (. ) BitFlags ( Sudoku ).

.NET ; , .

+2

, .

2

number<<power;

number*2^power

, , 2:

number>>power;

- .

Regex re = new Regex(".",RegexOptions.Multiline|RegexOptions.Singleline);

RegexOptions.Multiline|RegexOptions.Singleline i.e , .

- :

enum RegexOptions {
  Multiline  = (1 << 0), 
  Singleline = (1<<1)
};
+2

- . , .

/. . . , , .

, . , , , .

0

All Articles