Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

--Using [VS C#.NET]--

 

 

Given an Integer value (nOffset) which originates from a 2-byte message.

I need to do the following:

- Extract the Least Significant byte (LSB) from the Integer (nOffset) and store it in an array (baArray[0]) by bitwise-anding nOffset with 0x0F (0F in HEX)

- Bitwise-Shift the Integer (nOffset) by 8 (to move the MSB to the LSB of the Integer)

- Extract the new Least Significant byte (which was originally the MSB) and store it in the array (baArray[1]) by bitwise-anding nOffset with 0x0F (0F in HEX)

 

 

Currently I have the following code:

 

:: CODE ::

// Load LSB and MSG in Byte-Array

baArray[0] = nOffset & 0x0F;

nOffset = nOffset >> 8;

baArray[1] = nOffset & 0x0F;

 

But this isn't working, I keep getting the following error:

Project.cs(61): Cannot implicitly convert type 'int' to 'byte'

 

So I tried casting it as a byte like this;

baArray[0] = (byte)nOffset & 0x0F;

But it gives the same error.

 

Any clues or help would be appreciated.

Thanks,

Posted
when you use LSB are you implying that the B means Byte or B means Bit? I belive you are talking about byte... I am not sure what it all calls for, but if you aren't dealing with negative numbers you can use an unsigned 16-bit integer; right now you're using a signed 32-bit integer... See the difference. You have the right idea though.
-Sean
Posted

Yes my "b' means BYTE although I am still trying to do bitwise-and'ing.

I know I am on the right track, question is why does my code not work?

 

How would using a unsigned 16-bit integer help me? (altough I don't see how it would hurt).

 

Currently my nOffset works like:

int nOffset = 0;

Int16.Parse(nOffset, ...);

 

Does that help?

Because I am still kind of lost...

Posted

You said you are using all these bitwise operations on 2-byte values. So if you use a 4 byte value (32-bit integer) and attempt bitwise shifting, you're not going to get the right answer unless yuo use it for shifting a 2-byte value inside of a 4-byte data type. Example:

 

1: 16-bit unsigned number

1100010100101011 = 50475

 

shifting bits to the left 8 will result in

0010101111000101 = 11205

 

2: 32-bit unsigned number

00000000000000001100010100101011 = 50475 which is the ssme as above, but when we shift left 8 bits...

 

00000000110001010010101100000000 = 12921600 which isn't the same.

 

The MSByte in this case is 00000000. and the LSByte = 00000000 which is different from the 16-bit data type.

 

Take into account that these are unsigned values which means the sign bit is being used a regular value. If you would have a signed value, would you include the sign bit as you shift left and right? You'd have to figure out how you wanted to handle this before moving on I would think.

-Sean

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...