先介绍循环位移函数
#include <reg52.h>
#include <intrins.h> //还需要引入这个头文件
// 字符型循环左移:_crol_
//字符型循环右移:_cror_
void test1()
{
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
unsigned char e;
a = 0xfe; //1111 1110
b = _crol_(a, 1); //1111 1101
c = _crol_(a, 2); //1111 1011
d = _cror_(a, 1); //0111 1111
e = _cror_(a, 2); //1011 1111
}
// 注意对比位移运算符
void test2()
{
unsigned char a;
unsigned char b;
unsigned char c;
a = 0xfe; //1111 1110
b = a << 1; //1111 1100 右边自动补0
c = a >> 1; //0111 1111 左边自动补0
}
流水灯代码
#include <reg52.h>
#include <intrins.h> //还需要引入这个头文件
#define uint unsigned int
#define uchar unsigned char
void delay(unsigned int z)
{
unsigned int x, y;
for(x = z; x > 0; x--)
for(y = 114; y >0; y--);
}
void main()
{
//变量初始化
uchar temp;
temp = 0xfe;
P1 = temp;
delay(100);
while(1)
{
temp = _crol_(temp, 1);
P1 = temp;
delay(100);
}
}
网友评论