Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

"Assume that the variable X contains the 8-bit pattern abcdefgh, where each letter stands for the value of the bit in the corresponding position. E.g., if the bit pattern is 11001010, then a=1, b=1, c=0, d=0, etc. Write a single line of C code to change the value of X to abcd1fgh". How do I do this?

OpenStudy (e.mccormick):

Look into bit fields. That might be what you want.

OpenStudy (anonymous):

Thanks for the suggestion, but for the time being, would you be able to help me figure this specific one out?

OpenStudy (e.mccormick):

I do not work with C much and that is the only thing I can think of that might do what you need.

OpenStudy (anonymous):

Unfortunately, I have to submit this in an hour.

OpenStudy (lyrae):

This looks like a typical case for use of bit masking. To mask one or several bits you use a bit-wise logic operation (not, and, or, xor) and a mask of equal length. Each mask have different properties when combined with a bit-wise logic operation, for example *and* copies 1 and sets zeros to zero whilst *or* copies zeros and set ones to one. Example ``` abcdefgh and 01111111 ------------------ 0bcdefgh ``` More on this topic if you follow the link: http://www.c4learn.com/c-programming/c-bitwise-masking/

OpenStudy (e.mccormick):

How is a bitwise mask going to assign each bit to a different variable?

OpenStudy (lyrae):

I interpreted it as X is the only variable and abcdefgh is a generalized 8-bit pattern in X. The assignment would be to change the value of X to a new 8-bit pattern abcd1fgh using a single line of code.

OpenStudy (e.mccormick):

Hmmm... Yes, it could be that, which is certainly easier to do.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!