uint和int的区别
总结:
int是带符号的,表示范围是:-2147483648到2147483648,即-2^31到2^31次方。
uint则是不带符号的,表示范围是:2^32即0到4294967295。
uint可以使用十进制,二进制,十六进制。
和long,ulong,float,double,decimal等预定义可以进行隐式转换。但是需要注意值是否在可转换的范围内,不然会出现异常。
The Uint keyword signifies an integral type that stores calues according to the size and ranges shown in the following table.
关键字表示一种整型类型,该类型根据下表显示的大小和范围存储值。
其中范围 4,294,967,295,其实是2^32-1,为什么要减1呢?其实是因为计算机语言中,是由0开始的。
Note:The uint type is not CLS-compliant. Use int whenever possible.
请注意:uint类型不符合CLS。请尽可能使用int。
CLS 表示的含义,就是Common Language Specification 公共语言规范。
Literals 文本
You can declare and initialize a uint variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7.0) a binary literal to it. If the integer literal is outside the range of uint (that is, if it is less than Uint32.MinCalue or greater than Uint32.MaxValue), a compilation error occurs.
你可以通过为其分配十进制文本,十六进制文本或(从C#7.0开始)二进制文本来声明和初始化uint变量。如果整数文本在uint范围之外(即,如果它小于Uint32.MinValue或大于Uint32.MaxValue),会发生编译错误。
In the following example, integers equal to 3,000,000,000 that are represented as decimal, hexadecimal, and binary literals are assigned to uint values.
在下面的实例中,表示为十进制,十六进制和二进制文本且等于3,000,000,000的整数被分配给uint值。
uint uintValue1 = 3000000000;
Console.WriteLine(uintValue1);
uint uintValue2 = 0xB2D05E00;
Console.WriteLine(uintValue2);
uint uintValue3 = 0b1011_0010_1101_0000_0101_1110_0000_0000;
Console.WriteLine(uintValue3);
// The example displays the following output:
// 3000000000
// 3000000000
// 3000000000
Remark: 备注:
You use the prefix 0x or 0X to denote a hexadecimal literal and the prefix 0b or 0B to denote a binary literal. Decimal literals have no prefix.
你可以使用前缀ox或者0X表示十六进制文本,使用0b或者0B来表示二进制文本,十进制文本是没有前缀的。
Starting with C#7.0, a couple of features have been added to enhance readability.
C#7.0 allows the usage of the underscore character, _, as a digit separator.
C#7.2 allows _ to be used as a digit separator for a binary or hexadecimal literal, after the prefix. Adecimal literal isn't permitted to have a leading underscore.
从C#7.0开始,添加了一些功能以增强可读性。
C#7.0允许将下划线字符(_)用作数字分隔符。
C#7.2允许将_用作二进制或十六进制文本的数字分隔符,位于前缀之后,十进制文本不能够有前导下划线。
There are some examples below:
uint uintValue1 = 3000000000;
Console.WriteLine(uintValue1);
uint uintValue2 = 0xB2D0_5E00;
Console.WriteLine(uintValue2);
uint uintValue3 = 0b1011_0010_1101_0000_0101_1110_0000_0000;
Console.WriteLine(uintValue3);
uint uintValue2 = 0x_B2D0_5E00;
Console.WriteLine(uintValue2);
uint uintValue3 = 0b_1011_0010_1101_0000_0101_1110_0000_0000;
Console.WriteLine(uintValue3);//The example displays the following output:
// 3000000000
// 3000000000
// 3000000000
// 3000000000
// 3000000000
Integer literals can also include a suffix that denotes the type. The suffic u or 'u' denotes either a uint or a ulong, depending on the numeric value of the literal. The following example uses the u suffix to denote an unsigned integer of both types. Note that the first literal is a uint because its value is less than Uint32.MaxValue, while the second is a ulong because its value is greater than Uint32.MaxValue.
整数文本还可以包含表示类型的后缀。后缀u或‘u’表示uint或ulong,具体取决于文本的数字值。下面的示例使用u后缀来表示这两种类型的无符号整数。请注意第一个文本为uint,因为其值小于Uint32.MaxValue,而第二个文本为ulong,因为其值大于Uint32.MaxValue.
object value1 = 4000000000u;
Console.WriteLine($"{value1} ({4000000000y.GetType().Name})");
object value2 = 6000000000u;
Console.WriteLine($"{value2} ({6000000000y.GetType().Name})");
If an integer literal has no suffix, its type is the first of the following types in which its value can be represented:
如果整数文本没有后缀,则其类型为以下类型中可表示其值的第一个类型。
int
uint
long
ulong
Conversions 转换
There is a predefined implicit conversion from uint to long, ulong, float, double, or decimal. For example:
存在从uint到long,ulong,float,double或decimal的预定义隐式转换。例如:
float myFloat = 4294967290;
There is a predefined implicit conversion from byte, ushort, or char to uint. Otherwise you must use a cast. For example, the following assignment statement will produce a compilation error without a cast:
存在从byte,ushort或char到uint的预定义隐式转换。否则必须使用转换。例如,如果不使用转换,一下赋值语句会生成编译错误。
long aLong = 22;
//Error -- no implicit conversion from long
uint uInt1 = aLong;
//OK -- explicit conversion:
uint uInt2 = (uint)aLong;
Notice also that there is no implicit conversion from floating-point types to uint. For example, the following statement generates a compiler error unless an explicit cast is used:
另外注意,不存在从浮点类型到uint类型的隐式转换。例如,除非使用显示强制转换,否则以下语句将生成编译器错误:
//Error -- no implicit conversion from double:
uint x = 3.0;
//OK -- explicit conversion:
uint y = (uint)3.0;
uint和int的区别相关推荐
- uint与int的区别
为什么与如何使用uint 开始时这里有几个无符号的整数,这没有什么问题,如果你深入研究机器码去看你会发现在任何设备上它们都不过是无符号整数.所有其他的值比如说int,float,bool,charac ...
- Go语言中的uint和int的区别
话不多说,先上图 简单说明一下 , 类型大小为1,说明字节是1 , 一个字节占八位 int8的范围就是-128~127 有符号 uint的范围就是0~255 无符号
- int long java_java long int的区别
java long int的区别 java中long和int都属于整型,为什么还要细分为long和int两种数据类型呢?这是因为它们代表的大小不一样.具体区别如下: 1.区别1 16位系统:long是 ...
- c语言uint赋值给int,如何在C#中将uint转换为int?
假设您只想从一种类型中提取32位并将其原样转储到另一种类型中: uint asUint = unchecked((uint)myInt); int asInt = unchecked((int)myU ...
- 你知道Integer和int的区别吗
最近小康面试,突然被面试官问道,说一下Integer和int的区别.额-可能平时就知道写一些业务代码以及看一些自己觉得比较高大上的东西,包括面试也看的一些Spring源码等,对于这种java特别基础的 ...
- NSNumber 以及NSInteger,NSNumber以及Int的区别
// NSNumber 之所以可以(只能)包装基本数据类型,是因为继承了NSValue; // NSNumber 把基本数据类型包装成一个对象类型(因为集合不能存放基本数类型) //初始化 NS ...
- const int 和INT const区别
const int 和INT const区别 2010-04-09 23:26 const int a = 5; int const b = 6; 没区别 指针的时候有区别,引用也有区别 指针的话 1 ...
- c++将int转换成string_Integer与int的区别 (== 与 equal)
Integer与int的区别 (== 与 equal) 先来看下Java中的8种基本类型和3种引用数据类型 8种基本数据类型:boolean byte int char long short floa ...
- integer 负数字符串比较_Integer与int的区别 (== 与 equal)
Integer与int的区别 (== 与 equal) 先来看下Java中的8种基本类型和3种引用数据类型 8种基本数据类型:boolean byte int char long short floa ...
- python中str和int区别_python中eval与int的区别浅析
python中eval和int的区别是什么?下面给大家介绍一下: 1.eval()函数 eval(<字符串>)能够以Python表达式的方式解析并执行字符串,并将返回结果输出.eval() ...
最新文章
- C语言对mysql数据库的操作
- 如何逐步打下(研究生/博士生阶段)深度学习的数学基础?
- R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:使用gt包可视化表格数据,使其易于阅读和理解、使用gtExtras包添加一个图,显示表中某一列中的数字
- [IE技巧] 查看HTTP 验证的用户名/密码
- Linux系统卸载Apache(阿帕奇)环境教程
- kali2020进入单模式_2021神途手游:超级变态单职业
- 回顾 | Kubernetes SIG-Cloud-Provider-Alibaba 首次网研会(含 PPT 下载)
- 语言统计学中的几个定律,可作为设计检索的参考
- 面试时遇到「看门狗」脖子上挂着「时间轮」,我就问你怕不怕?
- maven snapshot和release版本号之间的差
- JSON(4)---JSON.parse()
- 堆排序--采用快速排序(利用大堆实现升序,小堆实现降序)
- ZZW_shell脚本中的调用MYSQL传参及注意的问题
- fanuc机器人与示教器配对_看FANUC机器人在重力浇铸行业,都是又累又重的活儿啊!...
- jQuery-EasyUI
- 闭包、作用域链、函数
- 《算法图解》第八章之贪婪算法
- linux内核nvme驱动程序,Linux中nvme驱动详解
- 如何将十进制转化为二进制和十六进制
- hexo博客可以在百度谷歌搜索到
热门文章
- pos机v1,简单pos机
- [Markdown][HTML]特殊符号
- wps公式如何加序号_Microsoft Word 插入公式、公式编号及交叉引用完美指南
- Meta-Tracker: Fast and Robust Online Adaptation for Visual Object Trackers
- 一阶电路实验报告心得_一阶动态电路的响应测试实验报告
- Vue 安装@vue/cli报错npmERR gyp ERR
- spa文件转换html,前端微服务化解决方案2 - Single-SPA
- 想用好低代码这把“双刃剑”,先搞清楚这三个问题|低代码系列(四)
- ssh 整合TOMCAT启动遇到错误
- 必修三计算机选修三知识点总结,高中生物必修三知识点总结