首页 > 其他 > 详细

CC150 19.4

时间:2014-12-10 14:20:42      阅读:207      评论:0      收藏:0      [点我收藏+]

19.4 Write a method which finds the maximum of two numbers. You should not use if-else or any other comparison operator. 

int max(int a, int b)
{
  int[] temp = {a, b};
  
  // If a > b, (a - b) >> 31 will be 0...000000;
  // Else, it will be 11111111..1; 
  // Thus, 
  int k = ((a - b) >> 31) & 1;  
  return temp[k];
}


CC150 19.4

原文:http://7371901.blog.51cto.com/7361901/1588281

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!