I am trying to learn C after working with Java for a couple of years. I've found some code that I wanted to reproduce which looked something like this:
U64 attack_table[...]; // ~840 KiB
struct SMagic {
U64* ptr; // pointer to attack_table for each particular square
U64 mask; // to mask relevant squares of both lines (no outer squares)
U64 magic; // magic 64-bit factor
int shift; // shift right
};
SMagic mBishopTbl[64];
SMagic mRookTbl[64];
U64 bishopAttacks(U64 occ, enumSquare sq) {
U64* aptr = mBishopTbl[sq].ptr;
occ &= mBishopTbl[sq].mask;
occ *= mBishopTbl[sq].magic;
occ >>= mBishopTbl[sq].shift;
return aptr[occ];
}
U64 rookAttacks(U64 occ, enumSquare sq) {
U64* aptr = mRookTbl[sq].ptr;
occ &= mRookTbl[sq].mask;
occ *= mRookTbl[sq].magic;
occ >>= mRookTbl[sq].shift;
return aptr[occ];
}
The code is not that important but I already failed at using the same datatype: U64
, I only found uint64_t
. Now I would like to know where the difference in U64
, uint64_t
and long
is.
I am very happy if someone could briefly explain this one to me, including the advantage of each of them.
Greetings, Finn
TL;DR - for a 64-bit exact width unsigned integer, #include <stdint.h>
and use uint64_t
.
Presumably, U64
is a custom typedef for 64-bit wide unsigned integer.
If you're using at least a C99 compliant compiler, it would have <stdint.h>
with a typedef
for 64-bit wide unsigned integer with no padding bits : uint64_t
. However, it might be that the code targets a compiler that doesn't have the standard uint64_t
defined. In that case, there might be some configuration header where a type is chosen for U64
. Perhaps you can grep
the files for typedef.*U64;
long
on the other hand, is a signed type . Due to various undefined and implementation-defined aspects of signed math, you wouldn't want to use a signed type for bit-twiddling at all. Another complication is that unlike in Java, the C long
doesn't have a standardized width; instead long
is allowed to be only 32 bits wide - and it is so on most 32-bit platforms, and even on 64-bit Windows. If you ever need exact width types, you wouldn't use int
or long
. Only long long
and unsigned long long
are guaranteed to be at least 64 bits wide.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.