Home > Enterprise >  C Set whole byte if first bit is 1
C Set whole byte if first bit is 1

Time:04-15

Hello Im trying to make program that will for for example 273(000100010001b) make to 4095(111111111111b).

Funny part is that my program works for first 2 iterations, for example 17(00010001b) correctly returns 255(11111111b) but every byte after that doesn't work. For instance 273(000100010001b) returns 61951(1111000111111111b) and I cant figure out why is that.

Here is my code

int x,temp, i;
int mask = 15;
scanf_s("%d", &x);
temp = x;
for(i=0;i<4;i  ){
    mask<<=i*4;
    if((temp&1)==1){
        x|=mask;
    } else{

    }
    temp>>=4;
    }
printf("%d", x);

CodePudding user response:

The issue is the shift you perform on mask. You shift it by 0 on the 1st iteration, 4 on the 2nd, 12 (4 8) on the 3rd and so on.

Also, you don't apply the mask over all the bits of temp. I can't tell if you do this on purpose.

Here you are a fixed version:

int foo(int x) {
/*  ^ I didn't know how to call such function */
    static size_t const mask_size = 4;
    unsigned mask = 15;

    int temp = x;
    for (unsigned i = 0; i < (CHAR_BIT * sizeof(int)) / mask_size;   i) {
        if ((temp & 1) == 1) {
            x |= mask;
        }
        mask <<= mask_size;
        temp >>= mask_size;
    }

    return x;
}

My results are:

in:  17(10) = 10001(2)
out: 255(10) = 11111111(2)
in:  273(10) = 100010001(2)
out: 4095(10) = 111111111111(2)
in:  4369(10) = 1000100010001(2)
out: 65535(10) = 1111111111111111(2)
  •  Tags:  
  • c
  • Related