Thread: Cast problem

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    Post 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

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kempelen View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    no, good try but it doesnt work

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Microsoft Visual C++ compiler, cast problem?
    By jonnie75 in forum C Programming
    Replies: 5
    Last Post: 11-10-2001, 08:53 AM