[March , 7, 2008] Watcom C 的笔记9 Watcom C (9)

          运算优先级的问题

    C语言中,“+ -”的优先级高于“<< >>”因此,下面这段程序无法得到期望值:

#include <stdio.h>
#include <stdint.h>
void main ()
{
	uint8_t		command_write=1;
	uint8_t		command_read=2;
	uint8_t		command_erase=3;
	uint8_t		command_read_status=4;

	uint64_t rst=0xFFFFFFFF;

	rst =       command_write+
			command_read<<8+
			command_erase<<16+
			command_read_status<<24;

	printf("[%08lX]\n",rst);
}	

     最简单的方法是将表达式改为下面的形式:

	rst =       command_write+
			(command_read<<8)+
			(command_erase<<16)+
			(command_read_status<<24);

					9:23 2008-1-21

发表评论

电子邮件地址不会被公开。 必填项已用*标注