chwi
chwi

Reputation: 2802

Simple left shifting with C#

I convert two strings from ASCII hex to byte

byte[] address = new byte[2];
int fullAddress;

address[0] = Convert.ToByte(stringZero, 16);
address[1] = Convert.ToByte(stringOne, 16);

fullAddress = (address[0] << 0xFFFF);
fullAddress |= address[1];

This yields a wrong output on the high niblle of fullAddress, low nibble is always correct. How should this be done correctly?

EDIT: The address should be a 32 bit value. E.g,

StringZero = 0x01
StringOne = 0x02
fullAddress = 0x0102

Upvotes: 0

Views: 418

Answers (1)

Gusdor
Gusdor

Reputation: 14334

You are shifting left by 0xFFFF, or 65535. The right hand operand of the left shift (<<) operator is the number of bits to shift. I think you meant 4.

fullAddress = (address[0] << 4);
fullAddress |= address[1];

Upvotes: 3

Related Questions