首页 > 技术文章 > 如何优雅的写C++代码(一)

naive 2014-02-26 12:15 原文

// get the greatest power of two that is a divisor of n:
	return n&-n;
	
	
// swap two integers a and b:
	a^=b^=a^=b;
	a^=b, b^=a, a^=b;
	
// check whether a (nature number > 1) is a power of two or not:
	return ( ! ( x & (x-1)));
	return (x&-x) == x;
	
// count number of bits set in v:
	for( int i=0; v; i++)
		v &= v - 1;
	
	

// copy two char array
	while(*s++ = *t++);
	
// binary search
	int left = 0, right = N;
	for( int mid=(left+right)>>1; right-left>1; mid = left + (right-left)>>1)
		(arr[mid] > x ? left : right ) = mid;
	return right;

推荐阅读