Question

Given the following code fragment: int * SW_Ptr; *SW_Ptr = 0x00000987; int Nib2 = (*SW_Ptr >>...

Given the following code fragment:

int * SW_Ptr;

*SW_Ptr = 0x00000987;

int Nib2 = (*SW_Ptr >> 8) & 0xF;

int Nib1 = (*SW_Ptr >> 4) & 0xF;

int Nib0 = (*SW_Ptr) & 0xF;

        

         Which is the correct value of Nib1, Nib2, Nib3.

  

Microcontrollers using c prog

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

Solution:

Given *SW_Ptr = 000000987 (Value is in hexadecimal format)

(a)

Finding the value of Nib2 :

=> As in the code given Nib2 = (*SW_Ptr >>8) & 0F;

Converting the hexadecimal values to binary :

=> *SW_Ptr = 000000987 = 0000 0000 0000 0000 0000 1001 1000 0111

=> 0F = 1111

We can see >> is a right shift operator so performing right shift with *SW_Ptr by 8 bits.

=> 0000 0000 0000 0000 0000 1001 1000 0111 >> 8 => 0000 0000 0000 0000 0000 0000 0000 1001

Now we can see & is bitwise AND operator so performing bitwise AND

=> 0000 0000 0000 0000 0000 0000 0000 1001 & 1111 => 0000 0000 0000 0000 0000 0000 0000 1001

So finally the result is 0000 0000 0000 0000 0000 0000 0000 1001 in binary so converting the result into hexadecimal.

=> 0000 0000 0000 0000 0000 0000 0000 1001 = 000000009

So the value of Nib2 = 000000009

(b)

Finding the value of Nib1:

=> As in the code given Nib1 = (*SW_Ptr >>4) & 0F;

Converting the hexadecimal values to binary :

=> *SW_Ptr = 000000987 = 0000 0000 0000 0000 0000 1001 1000 0111

=> 0F = 1111

We can see >> is a right shift operator so performing right shift with *SW_Ptr by 4 bits

=> 0000 0000 0000 0000 0000 1001 1000 0111 >> 4 => 0000 0000 0000 0000 0000 0000 1001 1000

Now we can see & is bitwise AND operator so performing bitwise AND

=> 0000 0000 0000 0000 0000 0000 1001 1000 & 1111 = 0000 0000 0000 0000 0000 0000 0000 1000

So finally the result is 0000 0000 0000 0000 0000 0000 0000 1000 in binary so converting the result into hexadecimal.

=> 0000 0000 0000 0000 0000 0000 0000 1000 = 000000008

So the value of Nib1 = 000000008

(c)

Finding the value of Nib3:

=> Given in the code its Nib0 and I think it's written by mistake so I am assuming it Nib3

Nib3 = (*SW_Ptr ) & 0F;

Converting the hexadecimal values to binary :

=> *SW_Ptr = 000000987 = 0000 0000 0000 0000 0000 1001 1000 0111

=> 0F = 1111

We can see & is bitwise AND operator so performing bitwise AND

=> 0000 0000 0000 0000 0000 1001 1000 0111 & 1111 = 0000 0000 0000 0000 0000 0000 0000 0111

So finally the result is 0000 0000 0000 0000 0000 0000 0000 0111 in binary so converting the result into hexadecimal.

=> 0000 0000 0000 0000 0000 0000 0000 0111 = 000000007

So the value of Nib3 = 000000007

I have explained each and every part of the question with the help of the operator understanding and performing the operations as well.

Add a comment
Know the answer?
Add Answer to:
Given the following code fragment: int * SW_Ptr; *SW_Ptr = 0x00000987; int Nib2 = (*SW_Ptr >>...
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