简体   繁体   中英

What is >> operator in c#

I am sure this has to as sweet and plain as butter. But I am not able get it or even find it.

It is related to colours in .net. I have taken a sample code from internet and trying to understand it. It takes a uInt as argument and do something to return a , r , g and b byte values. The method goes as:

private Color UIntToColor(uint color)
{
    byte a = (byte)(color >> 24);
    byte r = (byte)(color >> 16);
    byte g = (byte)(color >> 8);
    byte b = (byte)(color >> 0);
    return Color.FromArgb(a, r, g, b);
}

so what is >> here. For example,

color = 4278190335 // (blue color)

After processing

a = 255
r = 0
g = 0
b = 255

So can anyone help me to understand this?

It's in the docs

Right here

So, if you convert your value of 4278190335 to hex (because it's easier to see what's going on) you get 0xFF0000FF

So this line:

byte a = (byte)(color >> 24);

Will shift 0xFF0000FF 24 bits to the right to give you 0x000000FF. If you cast that to a byte, you will truncate off the most significant bits and end up with 0xFF or 255.

So you should be able to figure out what the other 3 lines do.

It's right-shift operator.

Basically, what it does is that it shifts all bits of the first operand to the right. The second operand specifies how "far" are bits shifted. For example:

uint value = 240; // this can be represented as 11110000
uint shift2 = value >> 2; // shift2 now equals 00111100
uint shift4 = value >> 4; // shift4 now equals 00001111

Good article on the subject is here .

>>是右移操作员。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM