Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 1110 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
« 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!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 339
Projects: 3
Exp: 16 years


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
« 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
« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

ctomni231 (33 views)
2014-07-18 06:55:21

Zero Volt (29 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (26 views)
2014-07-16 23:30:00

Cero (41 views)
2014-07-16 00:42:17

Riven (43 views)
2014-07-14 18:02:53

OpenGLShaders (31 views)
2014-07-14 16:23:47

Riven (30 views)
2014-07-14 11:51:35

quew8 (29 views)
2014-07-13 13:57:52

SHC (65 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!