2

I would like to know how to delete bits in a bit value.

I receive a 10 bits value (bit 0 to bit 9) and I have to send a variable which ignore bit 0, bit 2, bit 4 and bit 6 of the received value then my variable will be : bit 987531. How can I do ? I heard about mask bit I don't really know how to use it even if I know the mask would be 0x55

Thank you for helping me

3 Answers3

3

Create bits together manually like below:

//Option 1
uint8_t result = 0;
result |= (inputValue >> 1) & 1;
result |= ((inputValue >> 3) & 1) << 1;
result |= ((inputValue >> 5) & 1) << 2;
result |= ((inputValue >> 7) & 1) << 3;
result |= ((inputValue >> 8) & 1) << 4;
result |= ((inputValue >> 9) & 1) << 5;
unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
3

I smell embedded :) I do not promise portability (but for the most of compilers I know, It will do)

union
{
    struct
    {
        unsigned b0 : 1;
        unsigned b1 : 1;
        unsigned b2 : 1;
        unsigned b3 : 1;
        unsigned b4 : 1;
        unsigned b5 : 1;
        unsigned b6 : 1;

        unsigned bx : 3;
    }
    uint16_t raw;
}raw;

    raw.raw = value;

    uint8_t without_skipped = raw.b1 | (raw.b3 << 1) | (raw.b5 << 2) | (raw.bx << 3) ;

this is for little endian

For big endian reverse the struct and unnamed bitfild to padd

0___________
  • 60,014
  • 4
  • 34
  • 74
3

A solution that always uses 5 bits could be

new_value = ((data & 0x002) >> 1) |
            ((data & 0x008) >> 2) |
            ((data & 0x020) >> 3) |
            ((data & 0x080) >> 4) |
            ((data & 0x200) >> 5);

But here is another solution that instead of using a fixed number of bits (i.e. 5 in your case) uses a function that allows you to specify the number of bits to keep.

It could be something like:

#include <stdio.h>
#include <stdlib.h>

unsigned keepOddBits(const unsigned data, const unsigned number_of_bits_to_keep)
{
  unsigned new_value = 0;
  unsigned mask = 0x2;
  int i;
  for (i=0; i < number_of_bits_to_keep; ++i)
  {
    if (mask & data)
    {
      new_value = new_value | ((mask & data) >> (i + 1));
    }
    mask = mask << 2;
  }
  return new_value;
}

int main()
{
  printf("data 0x%x becomes 0x%x\n", 0x3ff, keepOddBits(0x3ff, 5));
  printf("data 0x%x becomes 0x%x\n", 0x2aa, keepOddBits(0x2aa, 5));
  printf("data 0x%x becomes 0x%x\n", 0x155, keepOddBits(0x155, 5));
  return 0;
}

will output:

data 0x3ff becomes 0x1f
data 0x2aa becomes 0x1f
data 0x155 becomes 0x0

Changing main to request 3 instead of 5 bits, like:

int main()
{
  printf("data 0x%x becomes 0x%x\n", 0x3ff, keepOddBits(0x3ff, 3));
  printf("data 0x%x becomes 0x%x\n", 0x2aa, keepOddBits(0x2aa, 3));
  printf("data 0x%x becomes 0x%x\n", 0x155, keepOddBits(0x155, 3));
  return 0;
}

will output:

data 0x3ff becomes 0x7
data 0x2aa becomes 0x7
data 0x155 becomes 0x0
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • @user8451061 I noticed you just accepted my answer. I was actually about to delete it because I thought it was wrong. My answer **only** keeps **odd numbered** bit. After reading your question one more time, it seems you want to keep bit number 8 (?). My code does **not** keep bit number 8. Are you sure that this is the answer you want to accept? – Support Ukraine Aug 18 '17 at 09:48