当前位置:网站首页>unsigned与signed之大白话
unsigned与signed之大白话
2022-06-25 22:06:00 【彼岸的饭碗】
闲谈
有符号与无符号,顾名思义,即有负号或无负号,unsigned即可定义无符号变量,即变量不会出现小于0的值,0-1此时会从上限值往下排列,因此我们看到了许多游戏中出现的一系列bug。
为了直观的看到他们的不同,我们直接上两盘小白代码
#include "stdio.h"
#include "iostream"
#define ull signed int
using namespace std;
int main(){
int n=10;
ull p=5;
while(n>0){
n--;
p--;
cout << p << '\n';
}
return 0;
}
输出结果:
#include "stdio.h"
#include "iostream"
#define ull unsigned int //这里有所不同
using namespace std;
int main(){
int n=10;
ull p=5;
while(n>0){
n--;
p--;
cout << p << '\n';
}
return 0;
}
输出结果:
无符号整型一般是常用于各种位运算、模运算、回绕溢出利用较多的算法实现中(比如各种加密学算法、编码、压缩算法等),或者是确实对数据表示范围有要求的场合(比如必须用16bit存储上限可以达到50000的量)。
一般应用中一个变量最好用有符号数(前提是这个变量不会和别的无符号变量有直接交互),即使是一些从意义上来说不会取负数的变量,可以避免一些容易疏忽的溢出漏洞,毕竟java基本类型中不支持无符号整型不是没有道理的。
例如你要倒序遍历某个数组的时候,有符号数可以直接写成
for(int i = size-1 ; i>=0 ; --i)但如果i是个无符号数,这么写就成了一个死循环。再比如《文明》中的核弹狂魔甘地就是由于无符号数的下溢出导致的。
边栏推荐
- Today's 61 Fu
- 第五章 习题(124、678、15、19、22)【微机原理】【习题】
- 达梦数据库修改字段信息采坑记
- .user.ini文件导致的php网站安装问题
- 二叉排序树
- The software test interview has been suspended. The interviewer always says that the logical thinking is chaotic. What should I do?
- Kylin
- YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
- 中序线索二叉树
- hiberate核心API/配置文件/一级缓存详解
猜你喜欢
Use of xinchida ble 5.0 Low Power Bluetooth module (at command serial port transparent transmission) rsbrs02abr
CSDN添加页内跳转和页外指定段落跳转
流数据
excel如何实现中文单词自动翻译成英文?这个公式教你了
Hbuilderx uses the gaude map to obtain the current location
line-height小用
QT Chinese and English use different fonts respectively
Leetcode-1528- rearrange string - hash table - string
森林的先序和中序遍历
社招两年半10个公司28轮面试面经(含字节、拼多多、美团、滴滴......)
随机推荐
对卡巴斯基发现的一个将shellcode写入evenlog的植入物的复现
产品经理如何把控产品开发的进度
Qlabel text scrolling horizontally
QT Chinese and English use different fonts respectively
Apache Doris1.0版本集群搭建、负载均衡与参数调优
中序线索二叉树
Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]
Binary, hexadecimal, big end and small end
php中使用Makefile编译protobuf协议文件
Once beego failed to find bee after passing the go get command Exe's pit
CSDN添加页内跳转和页外指定段落跳转
Online customer service - charging standards and service provision of third parties
C. Planar Reflections-CodeCraft-21 and Codeforces Round #711 (Div. 2)
Anaconda一文入门笔记
C. Fibonacci Words-April Fools Day Contest 2021
提取系统apk
期末复习【机器学习】
String object (constant) pool
第六章 习题(678)【微机原理】【习题】
Hbuilderx uses the gaude map to obtain the current location