Question

Language: C Write a function unsigned char tvect(unsigned char v) that accepts an unsigned byte value...

Language: C

Write a function unsigned char tvect(unsigned char v) that accepts an unsigned byte value and returns a transitioned byte with the following properties.

• bit7 = bit2 XOR bit3 /* That is, bit7 in the return byte is equal to bit2 in v xor’d with bit3 in v */

  • bit6

  • bit5

  • bit4

  • bit3

  • bit2 bit7

  • bit1

  • bit0

= bit0
= bit1 OR bit2
= bit4 AND bit7 AND (NOT bit0)
= NOT bit5
= bit0 XOR bit1 XOR bit2 XOR bit3 XOR bit4 XOR bit5 XOR bit6 XOR

= bit1 AND bit2 = bit5 OR bit6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The question formatting is illegible so I am assuming this is the question asked.

  • bit7 = bit2 XOR bit3
  • bit6 = bit0

  • bit5 = bit1 OR bit2

  • bit4 = bit4 AND bit7 AND (NOT bit0)

  • bit3 = NOT bit5

  • bit2 = bit0 XOR bit1 XOR bit2 XOR bit3 XOR bit4 XOR bit5 XOR bit6 XOR bit7

  • bit1 = bit1 AND bit2

  • bit0 = bit5 OR bit6

Program:

unsigned char pow2[8] = {1, 2, 4, 8, 16, 32, 64, 128}; //storing powers of 2

int ISSET(unsigned char v, int i) //the ith bit can be extracted by v AND 2^i i.e. v & pow2[i]
{
    return (v & pow2[i]) != 0;
}

void SET(unsigned char *v, int i) //ith bit can be set by v OR 2^i i.e. v | pow2[i]
{
    *v = *v | pow2[i];
}

unsigned char tvect(unsigned char v)
{
    unsigned char ret = 0;
    if(ISSET(v, 2) ^ ISSET(v, 3)) //condtion for 7th bit
        SET(&ret, 7);
    if(ISSET(v, 0)) //condtion for 6th bit
        SET(&ret, 6);
    if(ISSET(v, 1) || ISSET(v, 2)) //condtion for 5th bit
        SET(&ret, 5);
    if(ISSET(v, 4) && ISSET(v, 7) && !ISSET(v, 0)) //condtion for 4th bit
        SET(&ret, 4);
    if(!ISSET(v, 5)) //condtion for 3rd bit
        SET(&ret, 3);
    //condtion for 2nd bit
    if(ISSET(v, 0) ^ ISSET(v, 1) ^ ISSET(v, 2) ^ ISSET(v, 3) ^ ISSET(v, 4) ^ ISSET(v, 5) ^ ISSET(v, 6) ^ ISSET(v, 7))
        SET(&ret, 2);
    if(ISSET(v, 1) && ISSET(v, 2)) //condtion for 1st bit
        SET(&ret, 1);
    if(ISSET(v, 5) || ISSET(v, 6)) //condtion for 0th bit
        SET(&ret, 0);
    return ret;
}

Add a comment
Know the answer?
Add Answer to:
Language: C Write a function unsigned char tvect(unsigned char v) that accepts an unsigned byte value...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT