Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  C++ to Java and unsigned int64?  (Read 488 times)
0 Members and 1 Guest are viewing this topic.
Offline cryo75

Junior Newbie





« Posted 2013-03-13 08:38:37 »

Hi all,

Can this code snippet be converted to Java?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
u64 magic_table[24] = {
  0x1000000010000000ull,
  0x1000000000010000ull,
  0x1000000000000001ull,
  0x0100000001000000ull,
  0x0100000000010000ull,
  0x0100000000000010ull,
  0x0010000000100000ull,
  0x0010000000010000ull,
  0x0010000000000100ull,
  0x0001000010000000ull,
  0x0001000001000000ull,
  0x0001000000100000ull,
  0x0000100000000100ull,
  0x0000100000000010ull,
  0x0000100000000001ull,
  0x0000010000100000ull,
  0x0000010000001000ull,
  0x0000010000000100ull,
  0x0000001001000000ull,
  0x0000001000001000ull,
  0x0000001000000010ull,
  0x0000000110000000ull,
  0x0000000100001000ull,
  0x0000000100000001ull
};

u64 test(u64 magic_sum, int square) {
  u64 before = magic_sum & 0x4444444444444444ull;
  u64 afte = (magic_sum + magic_table[square]) & 0x4444444444444444ull;
  return after & ~before;
}
Offline Roquen

JGO Ninja


Medals: 66



« Reply #1 - Posted 2013-03-13 09:05:58 »

Change: u64 to long.  change: ull to L.
Offline cryo75

Junior Newbie





« Reply #2 - Posted 2013-03-13 09:27:58 »

I've already tried that approach but the function returns just long which I can't use instead of the bits.
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline princec
« League of Dukes »

JGO Kernel


Medals: 196
Projects: 3


Eh? Who? What? ... Me?


« Reply #3 - Posted 2013-03-13 11:30:27 »

What do you mean you can't use it? The C version returns a u64, which is just a "long" in Java (though Java's is signed)

Cas Smiley

Offline Roquen

JGO Ninja


Medals: 66



« Reply #4 - Posted 2013-03-13 11:47:58 »

It would be helpful if you gave an example of what you're not understanding how to do.
Offline ShannonSmith
« Reply #5 - Posted 2013-03-13 15:50:53 »

This won't work in java as there is no unsigned 64 bit integer. Using a regular signed long would usually work fine but you are doing addition and using all 64 bits which includes the sign bit. You could use java BigInteger but it will be slow and I suspect this is purely a performance hack so I suggest you just replace it with whatever you are actually trying to do.
Offline Roquen

JGO Ninja


Medals: 66



« Reply #6 - Posted 2013-03-13 16:21:11 »

There's no difference between signed and unsigned addition in 2's complement.
Offline VeaR

Junior Member





« Reply #7 - Posted 2013-03-13 19:30:48 »

This won't work in java as there is no unsigned 64 bit integer. Using a regular signed long would usually work fine but you are doing addition and using all 64 bits which includes the sign bit. You could use java BigInteger but it will be slow and I suspect this is purely a performance hack so I suggest you just replace it with whatever you are actually trying to do.


Java just wraps over int's and long's, so it is irrelevant. Bitwise it is irrelevant if its signed or unsigned, the addition works the same way (the CPU doesn't care if you consider a value as signed or unsigned). The only time when attention must be paid when porting code that deals with unsigned types is the right-shift operator ">>". If the original code works with unsigned types, then the unsigned-shift ">>>" must be used. The ">>" preserves the sign (the top bit), while the unsigned shift ">>>" shifts in a 0 to the topmost bit. But addition and subtraction works the same.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (94 views)
2013-05-17 21:29:12

alaslipknot (102 views)
2013-05-16 21:24:48

gouessej (132 views)
2013-05-16 00:53:38

gouessej (127 views)
2013-05-16 00:17:58

theagentd (138 views)
2013-05-15 15:01:13

theagentd (126 views)
2013-05-15 15:00:54

StreetDoggy (168 views)
2013-05-14 15:56:26

kutucuk (189 views)
2013-05-12 17:10:36

kutucuk (192 views)
2013-05-12 15:36:09

UnluckyDevil (199 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.115 seconds with 20 queries.