#include <stdio.h>
#include <math.h>
long max(int a);
void ConvetToBinary(int v);
void main()
{
int m,n;
int max_value;
int size_of_long=sizeof(long);
int flag=1;
max_value=pow(2.0,size_of_long*8-1)-1;
printf("max_value = %d (0X %08X) ",max_value,max_value);
printf("sizeof(long)=%d\n",size_of_long);
for(m=0;m<=30;m++)
{
n=max(m);
if(n<=max_value&&(n>>31==0)&&flag)
{
printf("%2d的阶层为 %11d (0X %08X) Right ",m,n,n);
ConvetToBinary(n);
}
else
{
flag=0;
printf("------------------------------------------------\n");
printf("%2d的阶层为 %11d (0X %08X) Wrong ",m,n,n);
ConvetToBinary(n);
}
}
}
long max(int a)
{
if(a==0) return 1;
else return a*max(a-1);
}
void ConvetToBinary(int v)
{
int nBits;
int i;
nBits=sizeof(int)<<3;
for(i=nBits-1;i>=0;i--)
{
printf("%d",(v>>i)&1);
}
printf("\n");
}
x*17=x*16+x 即x左移4位再与x相加
01110111011101011000000000000000 |<-- 16的阶乘为x
01110111010110000000000000000000 |<-- 左移4位(x<<4)
11101110110011011000000000000000 |<-- +x 补码
转换为10进制(负数的补码等于其绝对值的原码各位取反再加1):
11101110110011010111111111111111 |<-- -1
00010001001100101000000000000000 |<-- 取反(~)
即288522240(十进制) 添上符号 即-288522240