C语言--偶校验---位检测

来源:互联网 发布:mysql数据库修复表 编辑:程序博客网 时间:2024/05/29 18:33
#include <stdio.h>//数据结构体struct receiveData{unsigned  char cmd;//命令标志    unsigned  char cmd_info;//命令信息    unsigned  char option_first;//可选择的信息1    unsigned  char option_second;//可选择的信息2unsigned  char option_third;//可选择的信息1    unsigned  char option_four;//可选择的信息2    unsigned  char check;//校验unsigned  char end;//结束标志  暂时定位  0A 表示回车}receData={0x20,0xff,0xef,0x24,0x28,0x01,0x00,0x00};struct receiveData *stu=&receData;/********************************************************************************名称:LPC_Calculate_Parity()*功能:偶校验函数  返回1表示stu中有奇数个1;返回0则表示stu中正好有偶数个1*******************************************************************************/char LPC_Calculate_Parity(struct receiveData *str){char i=0,j=0,sum=0,check[6]={0};for(i=0;i<6;i++){switch(i){case 0:for(j=0;j<8;j++){if(((*str).cmd&(1<<j))!=0)check[i]++;};break;case 1:for(j=0;j<8;j++){if(((*str).cmd_info&(1<<j))!=0)check[i]++;};break;case 2:for(j=0;j<8;j++){if(((*str).option_first&(1<<j))!=0)check[i]++;};break;case 3:for(j=0;j<8;j++){if(((*str).option_second&(1<<j))!=0)check[i]++;};break;case 4:for(j=0;j<8;j++){if(((*str).option_third&(1<<j))!=0)check[i]++;};break;case 5:for(j=0;j<8;j++){if(((*str).option_four&(1<<j))!=0)check[i]++;};break;}sum+=check[i];printf("%d\n",check[i]);}if(sum%2==1)return 1;elsereturn 0;}void main(){LPC_Calculate_Parity(stu);}

原创粉丝点击