Shop OBEX P1 Docs P2 Docs Learn Events
Negative (byte) numbers...? — Parallax Forums

Negative (byte) numbers...?

DuckHeadDuckHead Posts: 7
edited 2006-11-16 18:20 in Propeller 1
Hi!
This is my first message i this forum, howdy ya'll!
I have had my eyes on the propeller for some time now, and when we were asked to
make a project in my realtimesystems course, I couldn't resist any longer. So here
I am with my demo board, and I just love it! tongue.gif
Anyhow, I'm working on a character recognition unit, based on an artificial neural network.
It's a fancy name, but it's basicaly just a couple of matrix manipulations. And it's here
my question arrises. I have a bunch of global defined arrays where I want to store both positive and
negative numbers. The range -128 to 127 (8-bits) would be perfect. But it seems like I can only get neg
numbers when using long. If I would use byte, I imagine that the matrix would only be a quarter of the long
sized verion. There must be something I'm doing wrong here...
I have now:
long  i_even[noparse][[/noparse]128] 'Will be filed with [noparse][[/noparse]-1 1 3 -3 2 ...]


I want:
byte  i_even[noparse][[/noparse]128] 'Will be filed with [noparse][[/noparse]-1 1 3 -3 2 ...]



Thanks!

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-11-16 18:20
    The way around this is to use sign extension and casting it into a long. So you would store it in byte format as you had shown, but when you use it you sign extend it "~i_even[noparse][[/noparse]i]", which takes bit 7 and copies it into bits 8-31. This casts the 8 bit sign number into a 32 bit number, if you operations do not overflow (go above 127 or below -128) the 8 right most bits of the 32 bit result will be the proper 8 bit answer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
Sign In or Register to comment.