Can somebody please explain PB's signed binary...

Just starting out? Need help? Post your questions and find answers here.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Can somebody please explain PB's signed binary...

Post by Kale »

I've been playing with binary numbers and im trying to get a handle on signed binary and trying to understand how PB represents negative numbers using binary.

I thought this was -5:

Code: Select all

%10000101
but its not, its -123 in PB.

I always though that to express a signed number in binary the left hand bit had to be 1 and the rest as normal depending on what type was used.

take this code:

Code: Select all

test.b = -3
Debug Bin(test)
Debug Bin(-3)
This is confusing the hell outta me, does PB convert all bytes to binary longs?

Anyone care to explain, i think im too tired to fit anymore in my head! :lol:
--Kale

Image
User avatar
Fou-Lu
Enthusiast
Enthusiast
Posts: 201
Joined: Tue Jul 12, 2005 8:30 am
Location: I'm pretty sure this is all a nightmare
Contact:

Post by Fou-Lu »

Wow that's really confusing! I too thought -5 was like that...

I read a thread about variables some days ago... bytes and words are converted into longs before being calculated. I really don't know much about that, but it seems that negative numbers are "inverted" positives so 3 would be %00000011, 3 is the fourth number so, -4 would be %11111100. Well, I think so... :roll:

~Fou-Lu (aka Lørd Cinneris (actually Elias Sant'Ana))

Image Image
CodeMonkey
User
User
Posts: 12
Joined: Tue Jul 12, 2005 6:55 pm
Location: United Kingdom

Post by CodeMonkey »

Seem to recall to represent a negative version of a number, you invert all the bits and add 1

Pretty sure it's called 'Two's Complement'
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

It looks like it is two's complement. To change the sign of a number with two's complement, take the number in binary form, invert (not) the bits and add 1, e.g.:

The byte 5 is %00000101, invert it to get %11111010, then add 1 to get the form for -5 which is %11111011

The negative of -5 should get us back to 5 so to check, -5 is %11111011, invert it %00000100, and add 1 to get %00000101.

If we find the -ve of %10000101 then we invert it to get %01111010, add 1 to get %01111011 which is 1+2+8+16+32+64=123 so the original number %10000101 was -123.

If the sign was changed by flicking the first (most significant) bit then there would be a +0 and a -0, losing out on one number.

To extend a number from a byte to a long the first bit is extended so the byte -5 is %11111011 but the long -5 is %11111111111111111111111111111011

But that leaves the question, why is

Code: Select all

test.b = -3
Debug Bin(test) 
extended to a long? I have no idea...
Mat
User avatar
Fou-Lu
Enthusiast
Enthusiast
Posts: 201
Joined: Tue Jul 12, 2005 8:30 am
Location: I'm pretty sure this is all a nightmare
Contact:

Post by Fou-Lu »

But that leaves the question, why is

Code: Select all

test.b = -3 
Debug Bin(test)  
extended to a long? I have no idea...
"test.b" is stored as a byte, but it's processed as a long. freak answered that here: viewtopic.php?t=15918 :wink:

~Fou-Lu (aka Lørd Cinneris (actually Elias Sant'Ana))

Image Image
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Fou-Lu wrote:"test.b" is stored as a byte, but it's processed as a long. freak answered that here: viewtopic.php?t=15918 :wink:
That makes sense, thank you :D
Mat
User avatar
Fou-Lu
Enthusiast
Enthusiast
Posts: 201
Joined: Tue Jul 12, 2005 8:30 am
Location: I'm pretty sure this is all a nightmare
Contact:

Post by Fou-Lu »

You're welcome. :oops:

By the way, it's kind of off topic, but could you answer a silly question:
How did you quote me like " Fou-Lu wrote: "? Everytime I try to quote someone I only get a normal " Quote: "... :?

~Fou-Lu (aka Lørd Cinneris (actually Elias Sant'Ana))

Image Image
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

I clicked on the quote button to the top right of your post (and deleted the text not needed in the quote). That automatically enters [quote="whoever is being quoted"] as the opening tag of the quotation :D
Mat
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Thanks all for clearing the binary question up, it all makes sense now :)

More Reading:
http://en.wikipedia.org/wiki/Two%27s_complement
--Kale

Image
Post Reply