C#,十六进制和有符号整数 [英] C#, hexadecimal notation and signed integers

查看:240
本文介绍了C#,十六进制和有符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个跟进问题

所以,Java软件商店在补码加到补,你可以做以下内容:

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 =选中((INT)0xFF000000);

int ALPHA_MASK = unchecked((int)0xFF000000);

编辑(添加):

要直接代表这个数字作为符号的值,你写

To "directly" represent this number as a signed value, you write

INT ALPHA_MASK = -0x1000000; // == -16777216

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.

这篇关于C#,十六进制和有符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆