I wrote a program which checks parity of 32-bit integer without bitwise operators. I can't also use ]
sign apart from declaration. My program is already good, but I don't know how can I make it work without [
sign. I tried many ways but my program doesn't want to compile. Here is my program:
#ifndef bit_set
#define bit_set
struct bit{
unsigned b0 : 1;
unsigned b1 : 1;
unsigned b2 : 1;
unsigned b3 : 1;
unsigned b4 : 1;
unsigned b5 : 1;
unsigned b6 : 1;
unsigned b7 : 1;
};
union bit_set
{
unsigned int x;
struct bit foo[4];
}word;
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "bit_set.h"
int main(void) {
printf("Input number: ");
if (scanf("%u", &word.x) == 0) {
printf("Incorrect input");
return 1;
}
int sum = 0;
for (int i = 0; i < 4; i++) {
sum += word.foo[i].b0 + word.foo[i].b1 + word.foo[i].b2 + word.foo[i].b3 + word.foo[i].b4 + word.foo[i].b5 + word.foo[i].b6 + word.foo[i].b7;
}
sum % 2 ? printf("NO") : printf("YES");
return 0;
}