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!

4
  • Look at link. You'll have to use bitmasks for what you want to achieve. Commented Sep 4, 2017 at 9:26
  • This answer is for a different question, but it should give you an example of generic way to set bits without struct bit-fields. Commented Sep 4, 2017 at 9:38
  • 1
    Why are you using C89 if you are learning C? Don't use old, outdated crap sources of learning. Commented Sep 4, 2017 at 9:44
  • You can use bitfield structure and union combinations to achive same. Commented Sep 4, 2017 at 10:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Set functions should mask out old values in w, if they are non-zero.
word_set_A should return (w & ~3) | (a & 3)
I have never heard of uint16_t. Is it compatable with ANSI C?
@DanShul There is formally nothing called "ANSI C" any longer, that term is usually used to refer to the old C89 standard. uint16_t was introduced with stdint.h in the year 1999. It is not compatible with C98 but with modern, standard C.
@DanShul An unsigned short will do if you can't use uint16_t.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.