0

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;
}  

2 Answers2

1

I can't also use ] sign apart from declaration

That doesn't make any sense. But in that case you can use trigraphs instead:

word.foo??(i??).b0

Or if you prefer digraphs:

word.foo<:i:>.b0
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

I wanted the code to not have so long printf and bitmap which is looking not good, so I implemented arrays. Unfortunately without [ sign it is impossible to do the task properly. Now I managed to do the task in an ugly way:

#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;
    unsigned b02 : 1;
    unsigned b12 : 1;
    unsigned b22 : 1;
    unsigned b32 : 1;
    unsigned b42 : 1;
    unsigned b52 : 1;
    unsigned b62 : 1;
    unsigned b72 : 1;
    unsigned b03 : 1;
    unsigned b13 : 1;
    unsigned b23 : 1;
    unsigned b33 : 1;
    unsigned b43 : 1;
    unsigned b53 : 1;
    unsigned b63 : 1;
    unsigned b73 : 1;
    unsigned b04 : 1;
    unsigned b14 : 1;
    unsigned b24 : 1;
    unsigned b34 : 1;
    unsigned b44 : 1;
    unsigned b54 : 1;
    unsigned b64 : 1;
    unsigned b74 : 1;
};
union bit_set
{
    unsigned int x;
    struct bit foo;
}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;
    sum += word.foo.b7+ word.foo.b6 +word.foo.b5 + word.foo.b4 + word.foo.b3 + word.foo.b2 + word.foo.b1 + word.foo.b0 + word.foo.b72 + word.foo.b62 + word.foo.b52 + word.foo.b42 + word.foo.b32 + word.foo.b22 + word.foo.b12 + word.foo.b02 + word.foo.b73 + word.foo.b63 + word.foo.b53 + word.foo.b43 + word.foo.b33 + word.foo.b23 + word.foo.b13 + word.foo.b03 + word.foo.b74 + word.foo.b64 + word.foo.b54 + word.foo.b44 + word.foo.b34 + word.foo.b24 + word.foo.b14 + word.foo.b04;
    sum % 2 ? printf("NO") : printf("YES");
    return 0;
}