有符号变量和无符号变量有什么区别? [英] What is the difference between signed and unsigned variables?

查看:337
本文介绍了有符号变量和无符号变量有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在C和C ++的上下文中看到了这些内容,但是有符号变量和无符号变量之间有什么区别?

I have seen these mentioned in the context of C and C++, but what is the difference between signed and unsigned variables?

推荐答案

有符号变量(例如有符号整数)将允许您表示正数和负数范围内的数字.

Signed variables, such as signed integers will allow you to represent numbers both in the positive and negative ranges.

无符号变量,例如无符号整数,将仅允许您以正数表示数字.

Unsigned variables, such as unsigned integers, will only allow you to represent numbers in the positive.

无符号和有符号变量(例如intbyte)都具有相同的范围(分别为65,536和256个数字),但是 unsigned可以表示比相应的带符号变量.

Unsigned and signed variables of the same type (such as int and byte) both have the same range (range of 65,536 and 256 numbers, respectively), but unsigned can represent a larger magnitude number than the corresponding signed variable.

例如,unsigned byte可以表示从0255的值,而signed byte可以表示-128127.

For example, an unsigned byte can represent values from 0 to 255, while signed byte can represent -128 to 127.

签名数字表示形式上的Wikipedia页面解释了位级别表示形式的区别,并且整数(计算机科学)页提供了每个有符号/无符号整数的范围表类型.

Wikipedia page on Signed number representations explains the difference in the representation at the bit level, and the Integer (computer science) page provides a table of ranges for each signed/unsigned integer type.

这篇关于有符号变量和无符号变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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