int到unsigned int转换 [英] int to unsigned int conversion

查看:2251
本文介绍了int到unsigned int转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int i = 

我非常惊讶地知道我无法通过转换将signed转换为unsigned int。 -62;
unsigned int j =(unsigned int)i;

我以为我已经知道这一点,因为我开始使用casts,但是我不能做!

解决方案

您可以将 int 转换为 unsigned int



由于值为负数,因此会向其中添加 UINT_MAX + 1 因此该值是有效的无符号数量。 (技术上,2 N 被添加到其中,其中N是用于表示无符号类型的位数。)



,因为您的平台上的 int 宽度为32位,从2 32 中减去62,得到4,294,967,234。


I'm just amazed to know that I can't convert signed to unsigned int by casting!

int i = -62;
unsigned int j = (unsigned int)i;

I thought I already knew this since I started to use casts, but I can't do it!

解决方案

You can convert an int to an unsigned int. The conversion is valid and well-defined.

Since the value is negative, UINT_MAX + 1 is added to it so that the value is a valid unsigned quantity. (Technically, 2N is added to it, where N is the number of bits used to represent the unsigned type.)

In this case, since int on your platform has a width of 32 bits, 62 is subtracted from 232, yielding 4,294,967,234.

这篇关于int到unsigned int转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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