473,414 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Type Conversion Problem

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
Nov 15 '05 #1
5 2283
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
"george r smith" <gs****@budgetext.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
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" <10*@100.com> wrote in message
news:eK*************@tk2msftngp13.phx.gbl...
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
"george r smith" <gs****@budgetext.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
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


Nov 15 '05 #3
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[i] = (byte)(((ui & mask) != 0)?1:0);
bitArray[i] = (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" <gs****@budgetext.com> wrote in message
news:ub**************@TK2MSFTNGP12.phx.gbl...
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" <10*@100.com> wrote in message
news:eK*************@tk2msftngp13.phx.gbl...
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
"george r smith" <gs****@budgetext.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
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



Nov 15 '05 #4
100,
strange name 100 but to each his own :)

It worked perfectly, thank you very much.
george

Nov 15 '05 #5

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.

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Aaron Watters | last post by:
I'm doing a heart/lung bypass procedure on a largish Python program at the moment and it prompted the thought that the methodology I'm using would be absolutely impossible with a more "type safe"...
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
1
by: Jimmy Seow | last post by:
Dear All, Hope somebody can help me with this. I have created a class and have got it to serialize and deserialize using XmlSerialization without any problems. I've also read the documentation...
11
by: RWC | last post by:
Hello, I'm having trouble converting code in Access XP / 2002. I have some code that declares an variable "as database" in Access 97, which is not recognized in Access XP. I've tried to find a...
4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
6
by: Dhirendra Singh | last post by:
Hi, The following C++ program is not compiling on my system. #include <iostream> using namespace std; class complex { double re, im; public: complex( ) :re(0), im(0) {}
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.