Visual C# Developer Center > Visual C# Forums > Visual C# Language > Another byte manipulation question
Ask a questionAsk a question
 

AnswerAnother byte manipulation question

  • Wednesday, November 04, 2009 3:46 AMlogan_spirit Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, I have another byte manipulation question: What is the effect of the following ? It is in the context of data recieved by a socket.

      sByteCt = 0;
      sByteCt = szBuf[2] & 0xff;
      sByteCt = sByteCt << 8;
      sByteCt = sByteCt | (szBuf[1] & 0xff);

    Thanks in advance.

Answers

  • Wednesday, November 04, 2009 5:27 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Take the 3rd byte and mask off all but the lower 8 bits (the low byte)
    Move those 8 bits to the left 8 bits (so they are now the high byte)
    Take the 2nd byte and mask off all but the lower 8 bits and combine this with the result from above.


    In other words, make the low byte of the 3rd array element the high byte, and the low byte of the 2nd array element the low byte.

    Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit

All Replies

  • Wednesday, November 04, 2009 5:27 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Take the 3rd byte and mask off all but the lower 8 bits (the low byte)
    Move those 8 bits to the left 8 bits (so they are now the high byte)
    Take the 2nd byte and mask off all but the lower 8 bits and combine this with the result from above.


    In other words, make the low byte of the 3rd array element the high byte, and the low byte of the 2nd array element the low byte.

    Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit