Convert 64bits signed to unsigned

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

HiddenSide
Posts: 3
Joined: Fri Mar 26, 2021 7:36 am
Full name: esteban gomez

Convert 64bits signed to unsigned

Post by HiddenSide »

Hello everyone.
I am trying to implement bitboards in a language that only has a 64 bit signed number.
I am following this tutorial:

When I try to represent an operation like this:

Code: Select all

set_bit (bitboard, square) (bitboard | = (1ULL << square))
I can't get the last bit because it is used to represent the sign of the number. One bit more and the error "provided value is too big" appears.
My question is how can I represent a bitboard with a number ranging from -9223372036854775808 to 9223372036854775807?
The language is gdscript from the Godot engine, and I think it has no function to convert from signed to unsigned.
I want to continue the tutorial but I am stuck with this.
JohnWoe
Posts: 491
Joined: Sat Mar 02, 2013 11:31 pm

Re: Convert 64bits signed to unsigned

Post by JohnWoe »

The 64th bit isn't a sign bit. It presents: -9223372036854775808
The largest number is 9223372036854775807

I don't know about that gdscript.
In Rust. Smt like this when types are fucked.

Code: Select all

28 |     b |= (1u64 << 3);
   |          ^^^^^^^^^^^ expected `i64`, found `u64`
So smt like this. If 1LL is signed int64_t. ULL is uint64_t

Code: Select all

set_bit (bitboard, square) (bitboard | = (1LL << square))
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Convert 64bits signed to unsigned

Post by mvanthoor »

JohnWoe wrote: Fri Mar 26, 2021 10:59 pm The 64th bit isn't a sign bit. It presents: -9223372036854775808
The largest number is 9223372036854775807
It is a sign bit. A signed int is stored as two's complement.

https://en.wikipedia.org/wiki/Two%27s_complement
I don't know about that gdscript.
In Rust. Smt like this when types are fucked.

Code: Select all

28 |     b |= (1u64 << 3);
   |          ^^^^^^^^^^^ expected `i64`, found `u64`
Nothing's "fucked" there. I assume "b" is an i64 because of the error. The part right of the equation is of type u64. 1u64 is u64, and the types for the shift operation must be compatible, so Rust derives that 3 must also be u64. The entire expression to the right is thus u64, and if "b" is i64, that's not compatible.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Convert 64bits signed to unsigned

Post by mvanthoor »

HiddenSide wrote: Fri Mar 26, 2021 9:26 am Hello everyone.
I am trying to implement bitboards in a language that only has a 64 bit signed number.
I am following this tutorial:

When I try to represent an operation like this:

Code: Select all

set_bit (bitboard, square) (bitboard | = (1ULL << square))
I can't get the last bit because it is used to represent the sign of the number. One bit more and the error "provided value is too big" appears.
My question is how can I represent a bitboard with a number ranging from -9223372036854775808 to 9223372036854775807?
The language is gdscript from the Godot engine, and I think it has no function to convert from signed to unsigned.
I want to continue the tutorial but I am stuck with this.
You can do some tricks to implement bitboards in a language that doesn't have unsigned ints.

Logic Crazy on YouTube explains this:
https://www.youtube.com/channel/UCmMjMH ... Zhxix-N-yg

Apparently Java also doesn't have unsigned 64-bit ints. (PS: I'd never use such a language to implement bitboards, except when in a very masochistic, self-torturing mood...)
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
HiddenSide
Posts: 3
Joined: Fri Mar 26, 2021 7:36 am
Full name: esteban gomez

Re: Convert 64bits signed to unsigned

Post by HiddenSide »

mvanthoor wrote: Sat Mar 27, 2021 12:24 am You can do some tricks to implement bitboards in a language that doesn't have unsigned ints.

Logic Crazy on YouTube explains this:
https://www.youtube.com/channel/UCmMjMH ... Zhxix-N-yg

Apparently Java also doesn't have unsigned 64-bit ints. (PS: I'd never use such a language to implement bitboards, except when in a very masochistic, self-torturing mood...)
My path in this bitboards is purely didactic, I don't intend to do anything serious for now. My idea is not to make a chess engine, I want to make a GUI taking advantage of all the flexibility that godot has for that. But the bitboards got in the way. I don't lose anything trying to make an engine prototype, besides I saw very good results in interpreted languages, such as javascript. In addition, it is a technique that can be applied to other board games.
The difference is that java, I think it has a function to convert from signed to unsigned. In gdscript as far as I know there is nothing like it so far. I only need a bit !!!
Thanks for answering mvanthoor, I'll take a closer look at that channel.
HiddenSide
Posts: 3
Joined: Fri Mar 26, 2021 7:36 am
Full name: esteban gomez

Re: Convert 64bits signed to unsigned

Post by HiddenSide »

JohnWoe wrote: Fri Mar 26, 2021 10:59 pm The 64th bit isn't a sign bit. It presents: -9223372036854775808
The largest number is 9223372036854775807

I don't know about that gdscript.
In Rust. Smt like this when types are fucked.

Code: Select all

28 |     b |= (1u64 << 3);
   |          ^^^^^^^^^^^ expected `i64`, found `u64`
So smt like this. If 1LL is signed int64_t. ULL is uint64_t

Code: Select all

set_bit (bitboard, square) (bitboard | = (1LL << square))
https://docs.godotengine.org/en/stable/ ... s_int.html
As mvanthoor said below, I also understand that the last bit represents the sign, if it is 1 the number is negative, if it is 0 the number is positive (or vice versa). It is the digit that I need to complete the 64 squares. With "<< 3" I don't see a way to convert that sign bit as part of the integer, since it is always there, no matter how much "shiftings" I use. As you can see, I am not understanding the example you show me. I'm going to review the types of rust to see if I can deduce your example. Thanks for answering.
JohnWoe
Posts: 491
Joined: Sat Mar 02, 2013 11:31 pm

Re: Convert 64bits signed to unsigned

Post by JohnWoe »

mvanthoor wrote: Sat Mar 27, 2021 12:21 am
JohnWoe wrote: Fri Mar 26, 2021 10:59 pm The 64th bit isn't a sign bit. It presents: -9223372036854775808
The largest number is 9223372036854775807
It is a sign bit. A signed int is stored as two's complement.

https://en.wikipedia.org/wiki/Two%27s_complement
I don't know about that gdscript.
In Rust. Smt like this when types are fucked.

Code: Select all

28 |     b |= (1u64 << 3);
   |          ^^^^^^^^^^^ expected `i64`, found `u64`
Nothing's "fucked" there. I assume "b" is an i64 because of the error. The part right of the equation is of type u64. 1u64 is u64, and the types for the shift operation must be compatible, so Rust derives that 3 must also be u64. The entire expression to the right is thus u64, and if "b" is i64, that's not compatible.
Semantics. I'm talking about the same thing.

Forget that Rust program. Just testing real type system. I like Rust because of cargo. Super fast to prototype stuff. I don't have to write Makefiles etc.

I wasn't criticizing Rust at all. I think even Linux will be rewritten in Rust. There's a huge push going on.
Then C language will go into trash bin. Where it belongs to.
Ras
Posts: 2488
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Convert 64bits signed to unsigned

Post by Ras »

JohnWoe wrote: Sat Mar 27, 2021 12:04 pmI think even Linux will be rewritten in Rust.
If you really think that about 30 million lines of code (and that's just the kernel) will be rewritten just because there is a nicer language, you might want to think again.
Rasmus Althoff
https://www.ct800.net
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Convert 64bits signed to unsigned

Post by mvanthoor »

Ras wrote: Sat Mar 27, 2021 12:11 pm If you really think that about 30 million lines of code (and that's just the kernel) will be rewritten just because there is a nicer language, you might want to think again.
Nah, they won't rewrite it. Rust should be 100% call-compatible with C, back and forth. There are talks to allow Rust to be used to new parts of the kernel, or to rewrite parts in Rust if rewriting is necessary. I can see C being phased out like that, but it may take 15 years to do it.

Even Microsoft is experimenting with Rust for new parts of Windows... but keeping to their ways, they're not going to use actual Rust, but create a language from scratch with similar features. If I remember correctly, it's called Project Verona, and I dislike it already,. (I also dislike Zig: it's exactly Rust, but with missing features because they are 'too difficult to understand.')

But enough about that. I don't know Godot, which seems to be a game engine... not directly my first choice to write a GUI in.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL