简体   繁体   中英

Activate some bits of a number in C

I need to implement the function int activate_bits(int a, int left, int right) that should 'activate' all the bits to the left of left and to the right of right on the number a (excluding the bits left and right).

So far I've came up with this:

int activate_bits(int a, int left, int right)
{
    int n, j, mask, masked_a;

    for (n = 1; n < left; n++) {
        mask = 1 << n;
        masked_a = a | mask;
    }

    for (j = 31; j > right; j--) {
        mask = 1 >> j;
        masked_a = a | mask;
    }

    return masked_a;
}

Can anyone help me to why the masked_a it's not correct or fix my faulty code?

I suspect you need to be setting masked_a to a before looping:

int activate_bits(int a, int left, int right)
{
    int n, j, mask, masked_a;

    masked_a = a;

    for (n = 0; n < left; n++) {
        mask = 0x80000000 >> n;
        masked_a = masked_a | mask;
    }

    for (j = 0; j < right; j++) {
        mask = 1 << j;
        masked_a = masked_a | mask;
    }

    return masked_a;
}

EDIT: I've updated the loops to what I think are correct, although that is open to interpretation depending on what the expected answer is. Apologies for the earlier incorrect response, I was just pointing out the glaring error and that should probably have been a comment.

  1. switch left and right in original code since left is highest bit and right is lowest bit.
  2. initialize masked_a to a
  3. change 1>>j to 1<<j

Assume your left and right is starting from 1

int activate_bits(int a, int left, int right)
{
    int n, j, mask, masked_a = a;

    for (n = 0; n < right - 1; n++) {
        mask = 1 << n;
        masked_a = a | mask;
    }

    for (j = 31; j > 31 - left + 1; j--) {
        mask = 1 << j;
        masked_a = a | mask;
    }

    return masked_a;
}

some functions to set, reset and write new value

unsigned set_bits(unsigned a, unsigned pos, unsigned size)
{
    unsigned mask = ((1 << size) - 1) << pos;
    return a |  mask;
}

unsigned reset_bits(unsigned a, unsigned pos, unsigned size)
{
    unsigned mask = ((1 << size) - 1) << pos;
    return a & ~(mask);
}


unsigned write_bits(unsigned a, unsigned newVal, unsigned pos, unsigned size)
{
    unsigned mask = ((1 << size) - 1);
    newVal &= mask;
    mask <<= pos;

    return (a & ~(mask)) | newVal << pos;
}

to make your units:

right = pos
left = pos + size - 1;

I had the same question. I've tried the solutions above, and none of them worked for me, since I think no one understood what you wanted exactly. I know it's been a year, but this worked for me, and I believe it's pretty simple:

int activate_bits(int a, int left, int right) {

    left = 31 - left;

    int i;
    for (i = 0; i < left; i++) {
        a = a | (0x80000000 >> i);
    }

    for (i = 0; i < right; i++) {
        a = a | (1 << i);
    }

    return a;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM