十六进制表示法和有符号整数 [英] Hexadecimal notation and signed integers
问题描述
这是一个后续问题.因此,Java存储在 two-s-complements 中的整数,您可以执行以下操作:>
This is a follow up question. So, Java store's integers in two's-complements and you can do the following:
int ALPHA_MASK = 0xff000000;
在C#中,这需要使用无符号整数uint
,因为它将其解释为4278190080
而不是-16777216
.
In C# this requires the use of an unsigned integer, uint
, because it interprets this to be 4278190080
instead of -16777216
.
我的问题是,如何在c#中以十六进制表示法声明负值,以及整数在内部如何精确表示?这里与Java有什么区别?
My question, how do declare negative values in hexadecimal notation in c#, and how exactly are integers represented internally? What are the differences to Java here?
推荐答案
C#(相反,.NET)也使用两者的补码,但它支持有符号和无符号类型(Java不支持).位掩码更自然地是无符号的东西-为什么一个位应该与所有其他位都不同?
C# (rather, .NET) also uses the two's complement, but it supports both signed and unsigned types (which Java doesn't). A bit mask is more naturally an unsigned thing - why should one bit be different than all the other bits?
在这种情况下,使用未经检查的演员表是安全的:
In this specific case, it is safe to use an unchecked cast:
int ALPHA_MASK = unchecked((int)0xFF000000);
要直接"将该数字表示为带符号的值,请输入
To "directly" represent this number as a signed value, you write
int ALPHA_MASK = -0x1000000; // == -16777216
十六进制与(或不应)与十进制没有任何区别:要表示一个负数,您需要写一个负号,后跟代表绝对值的数字.
Hexadecimal is not (or should not) be any different from decimal: to represent a negative number, you need to write a negative sign, followed by the digits representing the absolute value.
这篇关于十六进制表示法和有符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!