reverse.c

来自「c和指针 学习c语言必须阅读的书籍之一 提高对C语言的掌握理解能力」· C语言 代码 · 共 31 行

C
31
字号
/*
** Reverse the order of the bits in an unsigned integer value.
*/

unsigned int
reverse_bits( unsigned int value )
{
	unsigned int	answer;
	unsigned int	i;

	answer = 0;

	/*
	** Keep going as long as i is nonzero.  This makes the loop
	** independent of the machine's word size, hence portable.
	*/
	for( i = 1; i != 0; i <<= 1 ){
		/*
		** Shift the old answer to make room for the next
		** bit; then OR in a 1 if the value's last bit is
		** set; then shift the value to its next bit.
		*/
		answer <<= 1;
		if( value & 1 )
			answer |= 1;
		value >>= 1;
	}

	return answer;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?