Type Conversion Problem

G

george r smith

Hi all,

Need some help on a C# problem.
Playing with chess stuff - great fun to learn with.

int sq;
sq = 0;
UInt64 bb = 0;
sq = 1;
bb |= (1ul << sq);
sq = 6;
bb |= (1ul << sq);
sq = 57;
bb |= (1ul << sq);
sq = 62;
bb |= (1ul << sq);

Console.WriteLine(bb); // this prints 4,755,801,206,503,243,842

Now we have this ulong (UInt64) that has the following values in binary,hex
and decimal:
01000010 00000000 00000000 00000000 00000000 00000000 00000000 01000010
42 00 00 00 00 00 00 42
4,755,801,206,503,243,842

I know that it might not be the best way to go about chess programming but
what I want to do is to convert this ulong bb into either a regular array
using

(byte[] aByteArray = new byte[64])
or
a BitArray using (BitArray aBitArray = new BitArray(64).


Will someone please show me the way.
Thanks
grs
 
1

100

Hi george,
Try to use BitConverter.GetBytes static method. It has overload for UInt64
and it is what you need I believe.

HTH
B\rgds
100
 
G

george r smith

thanks but I tried that - GetBytes returns an 8 byte array - I want either
64 bits in a BitArray or an 64 byte array that will hold a 1 or 0 in it.

george

100 said:
Hi george,
Try to use BitConverter.GetBytes static method. It has overload for UInt64
and it is what you need I believe.

HTH
B\rgds
100
Hi all,

Need some help on a C# problem.
Playing with chess stuff - great fun to learn with.

int sq;
sq = 0;
UInt64 bb = 0;
sq = 1;
bb |= (1ul << sq);
sq = 6;
bb |= (1ul << sq);
sq = 57;
bb |= (1ul << sq);
sq = 62;
bb |= (1ul << sq);

Console.WriteLine(bb); // this prints 4,755,801,206,503,243,842

Now we have this ulong (UInt64) that has the following values in binary,hex
and decimal:
01000010 00000000 00000000 00000000 00000000 00000000 00000000 01000010
42 00 00 00 00 00 00 42
4,755,801,206,503,243,842

I know that it might not be the best way to go about chess programming but
what I want to do is to convert this ulong bb into either a regular array
using

(byte[] aByteArray = new byte[64])
or
a BitArray using (BitArray aBitArray = new BitArray(64).


Will someone please show me the way.
Thanks
grs
 
1

100

Oh, sory george I must have read the question more carefully.
So I don't know if there is ready for use solution in the framework.
BitConverter and BitArray's constructors obviously won't do.
So, I wrote it myself

ui= 0x4200000000000042ul;

//UInt64 mask = 1ul;// for little-endian order
byte[] byteArray = new byte[64];
BitArray bitArray = new BitArray(64);

//this is for index zero for the most significant bit;
UInt64 mask = 1ul << 63;
for(int i = 0; i < 64; i++)
{
byteArray = (byte)(((ui & mask) != 0)?1:0);
bitArray = (ui & mask) != 0;
mask >>= 1;
}

//To reverse the order reverse the for loop or move the mask the other
way.

HTH
B\rgds
100
george r smith said:
thanks but I tried that - GetBytes returns an 8 byte array - I want either
64 bits in a BitArray or an 64 byte array that will hold a 1 or 0 in it.

george

100 said:
Hi george,
Try to use BitConverter.GetBytes static method. It has overload for UInt64
and it is what you need I believe.

HTH
B\rgds
100
Hi all,

Need some help on a C# problem.
Playing with chess stuff - great fun to learn with.

int sq;
sq = 0;
UInt64 bb = 0;
sq = 1;
bb |= (1ul << sq);
sq = 6;
bb |= (1ul << sq);
sq = 57;
bb |= (1ul << sq);
sq = 62;
bb |= (1ul << sq);

Console.WriteLine(bb); // this prints 4,755,801,206,503,243,842

Now we have this ulong (UInt64) that has the following values in binary,hex
and decimal:
01000010 00000000 00000000 00000000 00000000 00000000 00000000 01000010
42 00 00 00 00 00 00 42
4,755,801,206,503,243,842

I know that it might not be the best way to go about chess programming but
what I want to do is to convert this ulong bb into either a regular array
using

(byte[] aByteArray = new byte[64])
or
a BitArray using (BitArray aBitArray = new BitArray(64).


Will someone please show me the way.
Thanks
grs
 
G

george r smith

100,
strange name 100 but to each his own :)

It worked perfectly, thank you very much.
george
 
J

Jeffrey Tan[MSFT]

Hi George,

Thanks for posting in this group.
To convert a ulong into a bit array, you can do like this:

UInt64 bb;
byte [] storearr=BitConverter.GetBytes(bb);
BitArray ba=new BitArray(storearr);

Because BitArray implemented IEnumerable interface, then you can use
IEnumerable.GetEnumerator() to get all the filed of bitarray

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top