惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
A
About on SuperTechFans
J
Java Code Geeks
G
Google Developers Blog
L
LangChain Blog
小众软件
小众软件
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
博客园 - 【当耐特】
IT之家
IT之家
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
博客园 - Franky
博客园_首页
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
Vercel News
Vercel News
B
Blog
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Integer Literal Type and Overflow In C++
2021-09-03 · via jdhao's digital space

I want to print the largest number that unsigned int can represent, which is 2^32 - 1. I use the following code:

unsigned int n = 2147483647 + 1 + 2147483647;

std::cout << n << std::endl;

However, I get an overflow warning from clangd:

overflow in expression; result is -2147483648 with type ‘int’

According to post here, the expression 2147483647 + 1 overflows because both number are treated as int type. The compiler will choose from int, long, long long in that order to find if a type can fit the number. The maximum positive integer an int type can represent is 2147483647. So the type chosen for 2147483647 is int, not long or long long. Thus we get the overflow warning when we add 1 to it. If you use 2147483648 directly, the compiler will choose long type for this number, which will also not overflow.

The variable type on the left side of the assignment does not impact the choose integer type for the variable on the right side. It only decides whether type conversion will happen when assigning the result to this variable. In this case, although we want to the assign the final result to an unsigned int type, 2147483647 is not converted to unsigned int automatically.

To fix issue, we can explicitly tell the compiler that we want an unsigned int instead of int:

unsigned int n = (unsigned int)2147483647 + 1 + 2147483647;
// or use the following
unsigned int n = 2147483647u + 1 + 2147483647;

By converting one number to unsigned int, the other number will also be elevated to unsigned int and no overflow occurs in this case.

References#