本文共 4400 字,大约阅读时间需要 14 分钟。
#includeint fits_bits(int x,int n){ int w=sizeof(int)<<3; return x == x<<(w-n)>>(w-n);}int main(){ printf("%d\n",fits_bits(114514,19));}
#includetypedef unsigned packed_t;int xbyte(packed_t word,int bytenum){ int w = sizeof(unsigned); int left = (w-bytenum-1)<<3; int right = (w-1)<<3; return (int)(word< >right);}int main(){ printf("%d\n",xbyte(-114514,0));}
#includevoid copy_int(int val,void* buf,int maxbytes){ if(maxbytes-(int)sizeof(val)>=0) memcpy(buf,(void*)&val,sizeof(val));}int main(){ int max = 114; void* buff = malloc(max); copy_int(514,&buff,114);}
#include#include int saturating_add(int x,int y){ int sum = x+y; int mask = INT_MIN; int pos_over = !(x&mask)&&!(y&mask)&&(sum&mask); int neg_over = (x&mask)&&(y&mask)&&!(sum&mask); (pos_over&&(sum=INT_MAX))||(neg_over&&(sum=INT_MIN));//这段是求教大佬得到的,实在精妙 return sum;}int main(){ printf("%x\n",saturating_add(0x10000000,-1));}
#include#include int tsub_ok(int x,int y){ int sub = x-y; return !((x>0&&y<0&&sub<0)||(x<0&&y>0&&sub>0));}int main(){ printf("%d\n",tsub_ok(114514,INT_MIN));}
#include#include int signed_high_prod(int x, int y) { int64_t mul = (int64_t) x * y; return mul >> 32;}unsigned unsigned_high_prod(unsigned x, unsigned y) { int sig_x = x >> 31; int sig_y = y >> 31; int signed_prod = signed_high_prod(x, y); return signed_prod + x * sig_y + y * sig_x;}unsigned test(unsigned x, unsigned y) { uint64_t mul = (uint64_t) x * y; return mul >> 32;}int main(){ printf("%d\n",test(114514,1919810)==unsigned_high_prod(114514,1919810));}
#include#include #include #include void* mcalloc(size_t nmemb,size_t size){ if(nmemb==0||size==0) return NULL; int tmp = nmemb*size; if(nmemb==tmp/size){ //注意不能与上一行合并写成nmemb==(nmemb*size)/size void* p = malloc(size); if(p) memset(p,0,nmemb); return p; } return NULL; }int main(){ printf("%d\n",mcalloc(INT_MAX,INT_MAX));}
#includeint mul17(int x){ return (x<<4)+x;}int mulm7(int x){ return x-(x<<3);}int mul60(int x){ return (x<<6)-(x<<2);}int mulm112(int x){ return (x<<4)-(x<<7);}int main(){ printf("%d\n",mul17(114514)==17*114514); printf("%d\n",mulm7(114514)==(-7)*114514); printf("%d\n",mul60(114514)==60*114514); printf("%d\n",mulm112(114514)==(-112)*114514);}
#include#include int divide_power2(int x,int k){ int sig = x&INT_MIN; (sig&&(x=x+(1< >k;}int main(){ printf("%d\n",divide_power2(-114514,3)==(-114514)/8);}
#include#include int divide_power2(int x,int k){ int sig = x&INT_MIN; (sig&&(x=x+(1< >k;}int mul3div4(int x){ x = (x<<1)+x; return divide_power2(x,2);}int main(){ printf("%d\n",mul3div4(114514)==114514*3/4);}
#include#include int threeforths(int x) { int sig = x & INT_MIN; int head = x & ~0x3; int tail = x & 0x3; int hdd4 = head >> 2; int hdd4m3 = (hdd4 << 1) + hdd4; int tlm3 = (tail << 1) + tail; int bias = (1 << 2) - 1; (sig && (tlm3 += bias)); int tlm3d4 = tlm3 >> 2; return hdd4m3 + tlm3d4;}int main(int argc, char* argv[]) { printf("%d\n",threeforths(-3)==-2);}
#includeint patternGenerator(int k){ return (-1<
#includeint patternGenerator(int k,int j){ return (~((-1)< <
#includeunsigned f2u(float x){ return *(unsigned*)&x;}int float_le(float x,float y){ unsigned ux = f2u(x); unsigned uy = f2u(y); unsigned sx = ux>>31; unsigned sy = uy>>31; return (ux<<1==0&&uy<<1==0)|| (sx&&!sy)|| (!sx&&!sy&&ux<=uy)|| (sx&&sy&&ux>=uy);}int main(){ printf("%d\n",float_le(-0,+0)); printf("%d\n",float_le(+0,-0));}
此段代码非原创,参见。
转载地址:http://dywai.baihongyu.com/