C# 如何按位操作十六进制值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10972850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is a Hex Value manipulated bitwise?
提问by Rusty Nail
I have a very basic understanding of bitwise operators. I am at a loss to understand how the value is assigned however. If someone can point me in the right direction I would be very grateful.
我对按位运算符有一个非常基本的了解。然而,我不知道如何分配价值。如果有人能指出我正确的方向,我将不胜感激。
My Hex Address: 0xE0074000
我的十六进制地址:0xE0074000
The Decimal value: 3758571520
十进制值:3758571520
The Binary Value: 11100000000001110100000000000000
二进制值:11100000000001110100000000000000
I am trying to program a simple Micro Controller and use the Register access Class in the Microsoft .Net Micro Framework to make the Controller do what I want it to do.
我正在尝试编写一个简单的微控制器并使用 Microsoft .Net 微框架中的注册访问类来让控制器执行我想要它做的事情。
Register T2IR = new Register(0xE0074000);
T2IR.Write(1 << 22);
In my above example, how are the bits in the Binary representation moved? I don't understand how the management of bits is assigned to the address in Binary form.
在我上面的例子中,二进制表示中的位是如何移动的?我不明白比特的管理是如何以二进制形式分配给地址的。
If someone can point me in the right direction I would be very greatfull.
如果有人能指出我正确的方向,我会非常高兴。
采纳答案by BerggreenDK
Forget about decimals for a start. You'll get back to that later.
首先忘记小数。你稍后会回到那个。
First you need to see the logic between HEX and BINARY.
首先你需要看看HEX和BINARY之间的逻辑。
Okay, for a byte you have 8 bits (#7-0)
好的,对于一个字节,您有 8 位(#7-0)
#7 = 0x80 = %1000 0000
#6 = 0x40 = %0100 0000
#5 = 0x20 = %0010 0000
#4 = 0x10 = %0001 0000
#3 = 0x08 = %0000 1000
#2 = 0x04 = %0000 0100
#1 = 0x02 = %0000 0010
#0 = 0x01 = %0000 0001
When you read that in binary, in a byte, like this one %00001000
当你以二进制形式读取它时,以字节为单位,就像这个 %00001000
Then the bit set, is the 4th from right aka bit #3 which has a value of 08 hex (in fact also decimal, but still forget about decimal while you figure out hex/binary)
然后设置的位是从右数第 4 位又名第 3 位,其值为 08 十六进制(实际上也是十进制,但在计算十六进制/二进制时仍然忘记十进制)
Now if we have the binary number %10000000 This is the #7 bit which is on. That has a hex value of 0x80
现在,如果我们有二进制数 %10000000 这是打开的#7 位。十六进制值为 0x80
So all you have to do is to sum them up in "nibbles" (each part of the hex byte is called a nibble by some geeks)
所以你所要做的就是将它们总结为“半字节”(十六进制字节的每个部分被一些极客称为半字节)
the maximum you can get in a nibble is (decimal) 15 or F as 0x10 + 0x20 + 0x40 + 0x80 = 0xF0 = binary %11110000
您可以在半字节中获得的最大值是(十进制)15 或 F 作为 0x10 + 0x20 + 0x40 + 0x80 = 0xF0 = 二进制 %11110000
so all lights on (4 bits) in a nibble = F in hex (15 decimal)
所以半字节中的所有灯(4位)=十六进制的F(十进制15)
same goes for the lower nibble.
较低的半字节也是如此。
Do you see the pattern?
你看到图案了吗?
回答by cyanic
Refer to @BerggreenDK's answer for what a shift is. Here's some info about what it's like in hex (same thing, just different representation):
请参阅@BerggreenDK 的回答,了解什么是转变。这里有一些关于十六进制的信息(同样的事情,只是不同的表示):
Shifting is a very simple concept to understand. The register is of a fixed size, and whatever bits that won't fit falls off the end. So, take this example:
移位是一个非常容易理解的概念。寄存器是固定大小的,任何不适合的位都从最后掉下来。所以,拿这个例子:
int num = 0xffff << 16;
int num = 0xffff << 16;
Your variable in hex would now be 0xffff0000. Note how the the right end is filled with zeros. Now, let's shift it again.
你的十六进制变量现在是0xffff0000. 注意右端是如何填充零的。现在,让我们再换一次。
num = num << 8;
num = num >> 8;
numis now 0x00ff0000. You don't get your old bits back. The same applies to right shifts as well.
num现在是0x00ff0000。你不会拿回你的旧物。这同样适用于右移。
Trick: Left shifting by 1 is like multiplying the number by 2, and right shifting by 1 is like integer dividing everything by 2.
技巧:左移 1 就像数字乘以 2,右移 1 就像整数除以 2。

