0

For the project i'm working on, a "word" is defined as 10 bit length, and as according to what my program does, I need to update specific bits in this word, with binary numbers (of course up to the limit of the length of the bits). My problem is that I don't know how to create these bits, and after that how to read them.

For example, "word" is set like this:

bits 0-1 - representing something A - can get values between 0-3.

bits 2-3 - representing something B - can get values between 0-3.

bits 4-5 - C - values 0-3.

bits 6-9 - D - values 0-15.

and as my program running, I need to decide what to fill in each group of bit. After that, when my word is completely full, I need to analyze the results, meaning to go over the full word, and understand from bits 0-1 what A is representing, from bits 2-3 what B is representing, and so on..

another problem is that bit number 9 is the most significant bit, which mean the word is filling up from bits 6-9 to 4-5 to 2-3 to 0-1, and later on printed from bit 9 to 0, and not as a regular array.

I tried to do it with struct of bit-fields, but the problem is that while a "word" is always 10 bits length, the sub-division as mentioned above is only one example of a "word". it can also be that the bits 0-1 representing something, and bits 2-9 something else.

I'm a bit lost and don't know how to do it, and I'll be glad if someone can help me with that. Thanks!

Nemus
  • 3,879
  • 12
  • 38
  • 57
Dan Shul
  • 7
  • 1
  • Look at [link](https://stackoverflow.com/questions/10090326/how-to-extract-specific-bits-from-a-number-in-c?rq=1). You'll have to use bitmasks for what you want to achieve. – Banex Sep 04 '17 at 09:26
  • [This answer](https://stackoverflow.com/a/19376742/694733) is for a different question, but it should give you an example of generic way to set bits without struct bit-fields. – user694733 Sep 04 '17 at 09:38
  • 1
    Why are you using C89 if you are learning C? Don't use old, outdated crap sources of learning. – Lundin Sep 04 '17 at 09:44
  • You can use bitfield structure and union combinations to achive same. – rajesh6115 Sep 04 '17 at 10:26

1 Answers1

1

Just model a "word" as an uint16_t, and set the appropriate bits.

Something like this:

typedef uint16_t word;

word word_set_A(word w, uint8_t a)
{
  w &= ~3;
  return w | (a & 3);
}

uint8_t word_get_A(word w)
{
  return w & 3;
}

word word_set_B(word w, uint8_t b)
{
  w &= ~0xc0;
  return w | ((b & 3) << 2);
}

... and so on.

unwind
  • 391,730
  • 64
  • 469
  • 606