当前位置:网站首页>NO.5整数的表示与运算
NO.5整数的表示与运算
2022-07-17 18:44:00 【L.Bubu】
目录
1.有符号数与无符号数的表示
●有符号数:第一位代表符号,1为负数,0为正数
●无符号数:没有符号代表正负。
●不同字长机器上的取值范围:‘’

UMax为无符号整数表示的最大值。
TMax、TMin分别表示有符号整数表示的最大值、最小值。
2.转换
●转换:

有符号数正数与无符号数相等;有符号数负数转化为无符号数加上2^w即可(w为机器字长);反之即减去2^w。
●C语言中的有符号数与无符号数:
▲常数
缺省状态下是有符号数
无符号数在后面添加U
0U, 4294967259U
▲转换
显示转换:
int tx, ty;
unsigned ux, uy;
tx = (int) ux;
uy = (unsigned) ty;
隐式转换:
tx = ux;
uy = ty;
●例:某32位机器,考虑以下C代码:
int x = –1;
unsigned u = 2147483648;
printf ( “x = %u = %d\n”, x, x);
printf ( “u = %u = %d\n”, u, u);
在32位机器上运行上述代码时,它的输出结果是什么?为什么?
答:
x = 4294967295 = –1
u = 2147483648 = –2147483648
▲因为–1的补码整数表示为“11…1”,作为32位无符号数解释时,其值为2^32–1= 4 294 967 296–1 = 4 294 967 295。
▲2^31的无符号数表示为“100…0”,被解释为32位带符号整数时,其值为最小负数:–2^(32-1) = –2^31 = –2 147 483 648。
3.拓展与截断
●拓展:将w位变成(w+k)位
无符号数直接补0,有符号数补符号位。
●截断:将w位变成(w-k)位
舍弃高位,保留低位
4.加法、移位
●无符号数加法:
操作数为w位,结果本应用(w+1)位储存,需丢弃进位。

●有符号数加法:
也需丢弃符号位,结果小于可以表示的最小的数为负溢出,大于可以表示的最大的数为正溢出。

正溢出和负溢出都会改变结果的符号。
●用移位运算代替乘法:
左移x位相当于乘以2^w,右移x位相当于除以2^w、
●使用移位代替除法时:
无符号数采用逻辑移位(左边补0):

有符号数采用算术移位(正数补0,负数补1):

边栏推荐
- 名片管理的框架搭建
- Responsive Zhimeng template logistics and freight service website
- Responsive dream weaving template wine cellar website
- Interviewer: is it acceptable to transfer to go?
- asterisk:No compatible codecs, not accepting this offer!
- [code hoof set novice village 600 question] calculate the number of digits of an integer
- [JS reverse crawler] - Youdao translation JS reverse practice
- 【码蹄集新手村 600 题】科学计数法的实现方式,输出指数形式
- [postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism
- How to upgrade Flink job gracefully?
猜你喜欢

国内外十大erp软件系统排名!

Principle of voice communication network

Onvif protocol related: 4.1.4 WS username token method to obtain the stream address

「技术播客月」Day 10: Meta Podcast: 聊聊播客这件事

Google Earth engine - 1992 present mixed coordinate ocean model, water temperature and salinity (global ocean data set HYCOM)
![[code hoof set novice village 600 question] calculate the number of digits of an integer](/img/4b/4c2b6889decd30bb2e0f3b28dee0cf.png)
[code hoof set novice village 600 question] calculate the number of digits of an integer

Google Earth Engine——1992—至今混合坐标海洋模型、水温和盐度(全球海洋数据集HYCOM)

ModuleNotFoundError: No module named ‘_distutils_hack‘

「津津乐道播客」#392 原汤话原食:仲夏夜,马砂、肉串儿、趿拉板儿

Galaxy Kirin V10 arm offline installation of portal
随机推荐
S32K148_ Can drive (bare metal development)
565.数组嵌套
onvif协议相关:2.1.3 none方式获取流地址
弹性负载均衡将访问流量自动分发到多台云服务器,扩展应用系统对外的服务能力,提高应用程序安全性。
Template virtual machine environment preparation
面试官:可以接受转Go吗?
[understanding of opportunity-46]: Guiguzi - Chapter 10 - schemer, meaning of wisdom
[code hoof set novice village 600 question] calculate the number of digits of an integer
AcWing第 60 场周赛
[postgraduate entrance examination vocabulary training camp] day 7 - second, attract, current, collect, simple, communicate, vocation
The cloud audit service CTS is a paid service. The paid items include the opening of tracker, event tracking, storage and retrieval of events within 7 days and other related fees
Responsive Zhimeng template logistics and freight service website
S32K148_CAN驱动(裸机开发)
onvif协议相关:2.1.2 none方式获取截图url
【码蹄集新手村 600 题】科学计数法的实现方式,输出指数形式
「津津樂道播客」#392 原湯話原食:仲夏夜,馬砂、肉串兒、趿拉板兒
【码蹄集新手村 600 题】格式化的输入输出,使用 0 来代替补全的空格
【码蹄集新手村 600 题】运算符 / 在不同的运算顺序中的类型转换
ssh无密钥登录
【码蹄集新手村 600 题】输出时的左对齐,右对齐