c - Get byte - how is this wrong? -


I get a named byte from 32 bit integer I am getting the wrong value but I do not know why.

There are restrictions of this problem:

The signed bits should be used, and I can not use multiplication. I especially need to know that the function What is wrong with

Here is the function:

  int retrieveByteFromWord (int word, int byte) {return (word & gt; & gt; (byte & lt; <3) ) & Amp; 0xFF; }   

ex: (3) (2) (1) (0) ------ Byte number in word: 10010011 11001100 00110011 10101000

I want To return byte 2 (11001100), get

Gettended word (word, 2) ---- returns: 11001100

But in some cases it is wrong and it will tell me That's not the case.

Any thoughts?


Here's the problem:

You have started working for a company that is implementing a set of procedures to work on the data structure where 4 signed bytes are packed in a 32 bit unsigned. Within the words, byte 0 (LSB) to 3 (MSB) are counted. You have been assigned the task of implementing a function for a machine using complementary arithmetic and arithmetic correct shifts of 2 with the following prototype:

  typedef unsigned packed_t int xbyte (packed_t word, int Bytenum);   

This is the previous employee who has been removed to get them wrong:

  int xbyte (packed word, int bytenum) {return ( Word & gt; & gt; (Battenam <<3) & amp; 0xFF; }   

A) What's wrong with the code? B) Write the correct implementation using only the left and right shift and a subtraction.

I have done B but I still do not know why this is wrong, because the decimal numbers are running like 12, 15, 19, 55 and then being packed in one word And then when I remove them, then they are not the same number ??? It may be that I am going to run some tests faster ...

Because This is homework. I will not give you a complete answer, but I will point you in the right direction. Your problem statement says that:

4 signed byte 32 bit Are packed in unsigned.

When you use bitwise & amp; 32 bit signed integer with 0xFF is the result of the most important bit - that is, symbol bit - is always 0 , so never original function


For example ...

When you say "Betformard (word, 2) ---- returns: 11001100" You are wrong.

Your return type is a 32 bit integer - is not an 8 bit integer You are not returning 11001100 You 00000000 00000000 00000000 11001100 . Returning

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -