首页 > 编程语言 >c和c++各种类型数据左移溢出对比

c和c++各种类型数据左移溢出对比

时间:2023-05-20 16:55:43浏览次数:36  
标签:main 各种类型 int 左移 unsigned c++ 111 long include

c int:

 1 #include<stdio.h>
 2 main(){
 3 //int
 4     int j=1;  //<<31==2147483648
 5             // 1<<32==1
 6             // (1<<32)-1==0
 7             // (1<<32)-2==4294967295
 8     for(int i=30;i<(1<<5)+1;i++)
 9         printf("1<<%d==%ld\n",i,j<<i);
10     printf("(1<<%d)-1==%ld\n",32,(j<<32)-1);
11     printf("(1<<%d)-2==%ld\n",32,(j<<32)-2);
12 }
View Code

1<<30==1073741824
1<<31==2147483648
1<<32==1
(1<<32)-1==0
(1<<32)-2==4294967295

 

c unsigned int:

 1 #include<stdio.h>
 2 main(){
 3 //unsigned int
 4     unsigned int j=1;//1<<31==2147483648
 5                     // 1<<32==1
 6                     // (1<<32)-1==0
 7                     // (1<<32)-2==4294967295
 8     for(int i=30;i<(1<<5)+1;i++)
 9         printf("1<<%d==%ld\n",i,j<<i);
10     printf("(1<<%d)-1==%ld\n",32,(j<<32)-1);
11     printf("(1<<%d)-2==%ld\n",32,(j<<32)-2);
12 }
View Code

1<<30==1073741824
1<<31==2147483648
1<<32==1
(1<<32)-1==0
(1<<32)-2==4294967295

 

c long:

 1 #include<stdio.h>
 2 main(){
 3 //long
 4     long j=1;//1<<63==-9223372036854775808
 5             // 1<<64==1
 6             // (1<<63)-1==9223372036854775807
 7             // (1<<64)-1==0
 8             // (1<<64)-2==-1
 9     for(int i=62;i<(1<<6)+1;i++)
10         printf("1<<%d==%ld\n",i,j<<i);
11     printf("(1<<%d)-1==%ld\n",63,(j<<63)-1);
12     printf("(1<<%d)-1==%ld\n",64,(j<<64)-1);
13     printf("(1<<%d)-2==%ld\n",64,(j<<64)-2);
14 }
View Code

1<<62==4611686018427387904
1<<63==-9223372036854775808
1<<64==1
(1<<63)-1==9223372036854775807
(1<<64)-1==0
(1<<64)-2==-1

 

c unsigned long:

 1 #include<stdio.h>
 2 main(){
 3 // unsigned long
 4     unsigned long j=1;//1<<63==-9223372036854775808
 5                     // 1<<64==1
 6                     // (1<<63)-1==9223372036854775807
 7                     // (1<<64)-1==0
 8                     // (1<<64)-2==-1    
 9     for(int i=62;i<(1<<6)+1;i++)
10         printf("1<<%d==%ld\n",i,j<<i);
11     printf("(1<<%d)-1==%ld\n",63,(j<<63)-1);
12     printf("(1<<%d)-1==%ld\n",64,(j<<64)-1);
13     printf("(1<<%d)-2==%ld\n",64,(j<<64)-2);
14 }
View Code

1<<62==4611686018427387904
1<<63==-9223372036854775808
1<<64==1
(1<<63)-1==9223372036854775807
(1<<64)-1==0
(1<<64)-2==-1

c完整测试代码:

 1 #include<stdio.h>
 2 main(){
 3 //int
 4     printf("int:\n");
 5     int j=1;  //<<31==2147483648
 6             // 1<<32==1
 7             // (1<<32)-1==0
 8             // (1<<32)-2==4294967295
 9     for(int i=30;i<(1<<5)+1;i++)
10         printf("1<<%d==%ld\n",i,j<<i);
11     printf("(1<<%d)-1==%ld\n",32,(j<<32)-1);
12     printf("(1<<%d)-2==%ld\n",32,(j<<32)-2);
13 // unsigned int
14     printf("\nunsigned int:\n");
15     unsigned int j2=1;//1<<31==2147483648
16                     // 1<<32==1
17                     // (1<<32)-1==0
18                     // (1<<32)-2==4294967295
19     for(int i=30;i<(1<<5)+1;i++)
20         printf("1<<%d==%ld\n",i,j2<<i);
21     printf("(1<<%d)-1==%ld\n",32,(j2<<32)-1);
22     printf("(1<<%d)-2==%ld\n",32,(j2<<32)-2);
23 //long
24     printf("\nlong:\n");
25     long j3=1;//1<<63==-9223372036854775808
26             // 1<<64==1
27             // (1<<63)-1==9223372036854775807
28             // (1<<64)-1==0
29             // (1<<64)-2==-1
30     for(int i=62;i<(1<<6)+1;i++)
31         printf("1<<%d==%ld\n",i,j3<<i);
32     printf("(1<<%d)-1==%ld\n",63,(j3<<63)-1);
33     printf("(1<<%d)-1==%ld\n",64,(j3<<64)-1);
34     printf("(1<<%d)-2==%ld\n",64,(j3<<64)-2);
35 // unsigned long
36     printf("\nunsigned long:\n");
37     unsigned long j4=1;//1<<63==-9223372036854775808
38                     // 1<<64==1
39                     // (1<<63)-1==9223372036854775807
40                     // (1<<64)-1==0
41                     // (1<<64)-2==-1    
42     for(int i=62;i<(1<<6)+1;i++)
43         printf("1<<%d==%ld\n",i,j4<<i);
44     printf("(1<<%d)-1==%ld\n",63,(j4<<63)-1);
45     printf("(1<<%d)-1==%ld\n",64,(j4<<64)-1);
46     printf("(1<<%d)-2==%ld\n",64,(j4<<64)-2);
47 }
View Code

int:
1<<30==1073741824
1<<31==2147483648
1<<32==1
(1<<32)-1==0
(1<<32)-2==4294967295

unsigned int:
1<<30==1073741824
1<<31==2147483648
1<<32==1
(1<<32)-1==0
(1<<32)-2==4294967295

long:
1<<62==4611686018427387904
1<<63==-9223372036854775808
1<<64==1
(1<<63)-1==9223372036854775807
(1<<64)-1==0
(1<<64)-2==-1

unsigned long:
1<<62==4611686018427387904
1<<63==-9223372036854775808
1<<64==1
(1<<63)-1==9223372036854775807
(1<<64)-1==0
(1<<64)-2==-1

 

c++如果输出用printf则所有结果与c相同。

下面是用std::cout<<;的输出方式的测试

c++完整测试代码:

标签:main,各种类型,int,左移,unsigned,c++,111,long,include
From: https://www.cnblogs.com/xuweihui/p/17417445.html

相关文章

  • C/C++家谱管理[2023-05-20]
    C/C++家谱管理[2023-05-20]家谱管理中国历史悠久,中华民族有五千年的文明史。从远古的神话传说时代、尧舜禹的禅让、夏商西周、东周春秋战国的百家争鸣、秦汉一统、三国战乱、魏晋南北朝的民族大融合、隋唐五代光辉灿烂的文化直到宋元明清帝制结束。五千年的历史留给我们的是无尽......
  • C++ Today01
    1.1C++初始#include<iostream>usingnamespacestd;intmain(){cout<<"HelloC++"<<endl;system("pause");return0;}1.单行注释#include<iostream>usingnamespacestd;//1.单行注释//2.多行注释intma......
  • c++ socket API使用流程(转)
    原文:https://blog.csdn.net/weixin_43687811/article/details/122657720?spm=1001.2101.3001.6650.16&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-16-122657720-blog-121686590.pc_relevant_multi_platform_featuressor......
  • c++STL—容器map/multimap
    1、map基本概念1.1、简介map中所有元素都是pairpair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)所有元素都会根据元素的键值自动排序1.2、本质map/multimap属于关联式容器,底层结构是用二叉树实现。1.3、优点可以根据key值快速找到value值1.4、map和multimap区别map不......
  • C++图书信息管理系统系统[2023-05-19]
    C++图书信息管理系统系统[2023-05-19]图书信息管理系统系统问题描述本图书信息管理系统包括图书的编号、书名、作者、分类号、出版单位、出版时间和价格,可实现新建图书信息管理文件,录入图书信息,查询图书信息,删除图书信息,浏览图书信息。类的设计:classreader//读者类class......
  • C++内存管理
    C/C++内存分配在一个程序的进程地址空间中,其内存分配如下:栈用来存储非静态局部变量、函数参数/返回值等,栈是向下增长的;堆用于程序的动态内存分配,堆是向上增长的;数据段用来存储全局数据和静态数据;代码段用来存储可执行指令,只读常量,字符串常量就存储在代码段中。数据段和代码段在语言......
  • c++打卡练习(35)
    求分母为40的所有真分数流程图:伪代码:源代码:#include<iostream>usingnamespacestd;intmain(){inti,j,num1,num2,t,n=0;for(i=1;i<40;i++){ num1=40; num2=i; while(num2!=0){ t=num1%num2; num1=num2; num2=t; } if(num1==1){ n++; ......
  • C++
    定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle(长方形)、Trapezoid(梯形)和Triangle(三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。#include<iostream>usingnamespacestd;cl......
  • C++ 如何快速实现一个容器的迭代器
    C++如何快速实现一个容器的迭代器引言C++的标准库中的容器都会提供迭代器,如果一个容器满足forward_range,那么这个容器一般会提供以下成员类型和函数:iteratorconst_iteratorbeginendbegincend如果该容器还满足bidirectional_range,那么该容器还会额外提供以下成员类型和......
  • 详解c++STL—容器set/multiset
    1、set基本概念1.1、功能所有元素都会在插入时自动被排序1.2、本质:set/multiset属于关联式容器,底层结构是用二叉树实现。1.3、set和multiset区别set不允许容器中有重复的元素multiset允许容器中有重复的元素2、set构造和赋值2.1、功能描述创建set容器以及赋值2.1、构造set<T>st;/......