码迷,mamicode.com
首页 > 移动开发 > 详细

csapp2e homework 2.77

时间:2014-12-11 22:30:47      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:blog   io   div   2014   log   ef   amp   as   size   

#include<stdio.h>
#define debug_print(x) printf(#x"\t%d\n",x)

int divide_power2(int x,int k)
{
	int w = sizeof(int) << 3;
	int sign = x >> (w - 1) ;
	int result;
	( sign == 0 && (result = (x >> k)) ) || ( sign == -1 && (result = ((x + (1 << k)-1)>> k)) );
	return  result;
}

int main()
{
	debug_print(100>>3);
	debug_print(divide_power2(100,3));
	debug_print(-100>>3);
	debug_print(divide_power2(-100,3));
	return 0;
}

csapp2e homework 2.77

标签:blog   io   div   2014   log   ef   amp   as   size   

原文地址:http://blog.csdn.net/u012769069/article/details/41874047

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!