简体   繁体   English

c#中的>>运算符是什么

[英]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. 它与.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. 它需要一个uInt作为参数并做一些事情来返回argb字节值。 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 所以,如果你将4278190335的值转换为十六进制(因为它更容易看到发生了什么)你会得到0xFF0000FF

So this line: 所以这一行:

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

Will shift 0xFF0000FF 24 bits to the right to give you 0x000000FF. 将向右移位0xFF0000FF 24位给你0x000000FF。 If you cast that to a byte, you will truncate off the most significant bits and end up with 0xFF or 255. 如果将其转换为一个字节,则将截断最高有效位并以0xFF或255结束。

So you should be able to figure out what the other 3 lines do. 所以你应该能够弄清楚其他3行的作用。

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 . 关于这个问题的好文章就在这里

>>是右移操作员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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