byte conversions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,
I've a byte value of 15...
i wan to get it's representation a follows
0x01 | 0x02|0x03|0x04


basically i want to know by OR ing which numbers,..am I getting a value of
15...

help me out..
 
Hi AVL,

I'm not entirely sure what you are seeking. Do you seek a method that will report what kind of bits represent a certain number?

15 = 1111
0001 = 0x01 = 1
0010 = 0x02 = 2
0100 = 0x04 = 4
1000 = 0x08 = 8

Given a number you can separete into bits using the shift operator >>

int shiftnumber = 1;
int number = 15;

while(number > 0)
{
if((number & shiftnumber) > 0)
// add number.ToString("x") somewhere (will report it as hexadecimal number)

shiftnumber <<= 1; // makes shiftnumber move one bit 0010 -> 0100
}

This code is untested, but might give you what you need with a little bit of tweaking.
 
Back
Top