Signed to unsigned ints?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

This may be a basic question but how do you work with unsigned integers?

i have to peek 1 byte from a memory address, the value is $80 , 128 dec, but is stored as -128 signed.

i know how to display it with stru(), but i need to work with the unsigned value. how?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

Justin,

Signed vs. unsigned values are only a matter of how the value is interpreted by PureBasic. An unsigned byte of -128 and a signed byte of 128 both hold the same value, as you said.

The easiest way to convert a signed byte to an unsigned one is to add 256 if it is less than zero:

Code: Select all

If Byte < 0
  Byte + 256
EndIf
Eric
Originally posted by Justin

This may be a basic question but how do you work with unsigned integers?

i have to peek 1 byte from a memory address, the value is $80 , 128 dec, but is stored as -128 signed.

i know how to display it with stru(), but i need to work with the unsigned value. how?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

Thanks that works well.

so i need to copy the value into at least one higher memory type and do the math

i wonder how it is done in other languages.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

A much faster method:

A.l = Byte.b & $FF

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

Thanks fred, looks cleaner. but with longs i have to use the other method, i do this asumming byte , word and long are negative:

a.w= byte.b & $ff

a.l= word.w & $ffff

a.f= long.l + 4294967296
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by geoff.

Yes, Fred's method is fast, but it still slows things down.

Suppose you have a huge image array: pixel(x,y)
To increase the brightness you want to use:

Code: Select all

for x=1 to wd  for y=1 to ht
    pixel(x,y)=n * pixel(x,y)
  next y
next x
But you have to use:

Code: Select all

for x=1 to wd
  for y=1 to ht
    longpix.l=pixel(x,y) & $FF
    pixel(x,y)=n * longpix
  next y
next x
This slows things significantly for megapixel image processing. You could use word arrays, but this is unattractive because the byte arrays already require huge amounts of memory.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by Justin

a.f= long.l + 4294967296
A fairly old topic, but Geoff's post brought it to my pedantic eye. Andre already knows my answer to this :wink:

That code won't work (well). Floats have less accuracy than longs, so by doing this large values may lose some of the lower value digits. Whether this matters to your application is another story.


--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.62, external editor)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

I don't work to much with floats but having unsigned ingtegers would be nice, i guess is not easy to implement.
Post Reply