# Cast problem

• 06-16-2008
Kempelen
Cast problem
Code:

```#define C64 unsigned long long C64 hash_rand() {         C64 r = 0;         unsigned long r1, r2= 0;         for (unsigned int i = 0; i < 32; ++i) {                 r1 ^= rand() << i;                 r2 ^= rand() << i;         }         r = r1 | (C64) r2 << 32;         printf("R1=%ul R2=%ul Hash_rand=%ull\n",r1,r2,r);         return r; }```
A few result:
Code:

```R1=687865691l R2=988029917l Hash_rand=687865691ll R1=591616934l R2=454174817l Hash_rand=591616934ll R1=930215157l R2=214394930l Hash_rand=930215157ll R1=2936190921l R2=1135713105l Hash_rand=2936190921ll R1=1256449038l R2=885147166l Hash_rand=1256449038ll R1=1912210059l R2=2292338960l Hash_rand=1912210059ll R1=1680048649l R2=2457515678l Hash_rand=1680048649ll R1=1436782960l R2=737207696l Hash_rand=1436782960ll R1=3008899124l R2=445239638l Hash_rand=3008899124ll R1=1501323191l R2=1232787536l Hash_rand=1501323191ll R1=1555984070l R2=213463547l Hash_rand=1555984070ll R1=278736328l R2=2676575472l Hash_rand=278736328ll R1=1041491509l R2=1880486007l Hash_rand=1041491509ll R1=2564025414l R2=3808829273l Hash_rand=2564025414ll R1=943291562l R2=3734505863l Hash_rand=943291562ll R1=1744278179l R2=3876748683l Hash_rand=1744278179ll R1=3977573745l R2=1631571305l Hash_rand=3977573745ll R1=2742987346l R2=1756414339l Hash_rand=2742987346ll```
Why I am not getting "hash_rand" as the combination of r1 and r2?
thx
FS
• 06-16-2008
C_ntua
Well, don't know exactly what an unsigned long long is. I suppose it has 64bits? And your long have 32 bits?

But in any case try this:

Code:

```.. r =(C64) r1 | (C64) r2 << 32; ..```
So r1 is also casted to a C64. What you where doing is probably wrong. You do an | between a C64 (casted r2) and a ulong r1. Cast as I said and tell me:)
• 06-16-2008
Kempelen
same problem. In fact the problem is not with (C64) r1, but with (C64)r2<<32 because this is 0 when assigned to r.
if I replace that line with r = (C64) r2 << 32;
I get 0 as result
I suspect the problem is with my compiler, because I have seen other programs that make this cast without problem.
• 06-16-2008
matsp
Quote:

Originally Posted by Kempelen
same problem. In fact the problem is not with (C64) r1, but with (C64)r2<<32 because this is 0 when assigned to r.
if I replace that line with r = (C64) r2 << 32;
I get 0 as result
I suspect the problem is with my compiler, because I have seen other programs that make this cast without problem.

You will probably find that it works if you do
Code:

`r = (C64)r2 << (C64)32;`
--
Mats
• 06-16-2008
Kempelen
no, good try but it doesnt work
• 06-16-2008
C_ntua
Are you sure the C64 type you have is a 64bit variable? I test it and it tells me it is a 32bit variable. That is why r = (C64) r2 << 32; gives zero. If it was 64bit then it should not give zero.

Try printf("%d", sizeof(C64));

By the way!
Does %ull exist for printf() ?? I think it doesn't.
Look at your results. Everything ends with a l. That is not a "1" but an "l" propably!
• 06-16-2008
matsp
Right, if you are using windows and gcc mingw, you need to use the %I64u rather than %ull - this can be seen by the fact that your number is showing with an extra l on the end [it would be more noticeable if you used %ULL, as L is more different 1 than a l.

--
Mats
• 06-16-2008
Kempelen
I change the program for this one:

Code:

```#define C64 unsigned long long #define putBitTo1(x,y)                ((x) |= ((BITBOARD) 1) << (y)); C64 hash_rand() {         C64 r = 0;         for (unsigned int i = 0; i < 64; ++i) {               if (rand()%2)                 putBitTo0(r,i);         }         return r; }```
And this Works!!!!.
Don't know where the problem is, but has a workaround