无符号类型的范围为0到2^(effective number of bits used by the type) - 1 有符号类型具有实现定义的最小值: 2的补码-(2^(effective number of bits used by the type - 1)) 所有其他-(2^(effective number of bits used by the type - 1) - 1) 有符号类型的最大值为2^(effective number of bits used by the type - 1) - 1 ^是幂函数,而不是异或。
/* Hope it helps
* C Program to Print the Range~~ Google
*/
#include <stdio.h>
#define SIZE(x) sizeof(x)*8
void signed_one(int);
void unsigned_one(int);
void main()
{
printf("\nrange of int");
signed_one(SIZE(int));
printf("\nrange of unsigned int");
unsigned_one(SIZE(unsigned int));
printf("\nrange of char");
signed_one(SIZE(char));
printf("\nrange of unsigned char");
unsigned_one(SIZE(unsigned char));
printf("\nrange of short");
signed_one(SIZE(short));
printf("\nrange of unsigned short");
unsigned_one(SIZE(unsigned short));
}
/* RETURNS THE RANGE SIGNED*/
void signed_one(int count)
{
int min, max, pro;
pro = 1;
while (count != 1)
{
pro = pro << 1;
count--;
}
min = ~pro;
min = min + 1;
max = pro - 1;
printf("\n%d to %d", min, max);
}
/* RETURNS THE RANGE UNSIGNED */
void unsigned_one(int count)
{
unsigned int min, max, pro = 1;
while (count != 0)
{
pro = pro << 1;
count--;
}
min = 0;
max = pro - 1;
printf("\n%u to %u", min, max);
}
2条答案
按热度按时间bvjxkvbb1#
无符号类型的范围为
0
到2^(effective number of bits used by the type) - 1
有符号类型具有实现定义的最小值:
2的补码
-(2^(effective number of bits used by the type - 1))
所有其他
-(2^(effective number of bits used by the type - 1) - 1)
有符号类型的最大值为
2^(effective number of bits used by the type - 1) - 1
^
是幂函数,而不是异或。4ktjp1zp2#
程序说明
1.将字节数乘以8,将字节数转换为位数。
1.使用两个函数signed_one()和unsigned_one()分别计算有符号和无符号数据类型的范围。
1.在步骤1中得到的值作为参数发送给两个函数。在两个函数中,它都由变量count接收。
1.在两个函数中将变量pro初始化为1。
1.在函数signed_one()中使用while循环,条件为(count!= 1),将变量pro向左移动1个位置,并连续将变量count递减1。
1.当循环终止时,将pro的补码赋给变量min,并将min递增1。将变量pro递减,并将其赋给变量max。打印min和max作为输出。
1.在函数unsigned_one()中使用while循环,条件为(count!=0),将变量pro向左移动1个位置,并连续将变量count递减1。
1.当循环终止时,将零赋给变量min。将变量pro递减并将其赋给变量max。输出min和max。