LoginLogin

8-bit signed?

Root / Programming Questions / [.]

SaladFingersCreated:
Is it possible to convert an integer to an 8bit signed integer? Edit: I guess I wasn’t trying hard enough, my integer was already 1 byte long, just needed to get rid of the most significant bit. Edit 2: I was wrong, I still need some help. I have a number, for example &B11111011 (251) and I need it to be represented as a negative, signed number.

For converting a signed number to a larger integer type (8 bit -> 32 bit) you can use the fact that >> preserves the highest bit:
X<<(32-8)>>(32-8)
for example,
&B11111011
<<24
&B11111011000000000000000000000000
>>24
&B11111111111111111111111111111011 (-5)

It is possible if the integer is in the range of -128 to 127. Could you refer to the following program?
ACLS
A%=256
WHILE A%<-128 || A%>127
 INPUT"AN INTEGER (-128 TO 127)";A%
WEND

B%=A%+256*(A%<0)

?"8BIT SIGNED INT OF ";:?A%
?" = ";:?"&B";:?BIN$(B%,8);
?"(&H";:?HEX$(B%,2);:?")"

I'm sorry I've lost your EDIT2 but it is very simple as follows.
A%=&B11111011
?A%-256*(A%>=128)