### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [solved] How to create a signed integer from bytes  (Read 1066 times)

#### MarkMLl

• Hero Member
• Posts: 2697
##### Re: How to create a signed integer from bytes
« Reply #15 on: April 15, 2021, 10:46:22 pm »
has no chance to get the endianness wrong.

...but it depends on whether your intention is to combine bytes and words (which do not necessarily represent specific numbers) or to combine numbers using arithmetic/logical operations. The distinction is obviously subtle, but is very much relevant when one is tinkering with e.g. CPU simulation.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### jamie

• Hero Member
• Posts: 4568
##### Re: [solved] How to create a signed integer from bytes
« Reply #16 on: April 15, 2021, 10:52:56 pm »
Code: Pascal  [Select][+][-]
1. MysmallInt := Int16(WORD((UpperByte shl 8)+LowByte));
2.

Seems I remember the logical shift operators don't work like you would think with integers.
The only true wisdom is knowing you know nothing

#### y.ivanov

• Full Member
• Posts: 125
##### Re: How to create a signed integer from bytes
« Reply #17 on: April 15, 2021, 11:12:28 pm »
has no chance to get the endianness wrong.

...but it depends on whether your intention is to combine bytes and words (which do not necessarily represent specific numbers) or to combine numbers using arithmetic/logical operations. The distinction is obviously subtle, but is very much relevant when one is tinkering with e.g. CPU simulation.

MarkMLl

@MarkMLI, I honestly confess, I didn't get you. The Muso's intent was to combine two bytes in a signed word (Int16). What do you mean by making the 'subtle' distinction between combining bytes/words and combining numbers? Can you please explain.

#### MarkMLl

• Hero Member
• Posts: 2697
##### Re: How to create a signed integer from bytes
« Reply #18 on: April 15, 2021, 11:55:14 pm »
@MarkMLI, I honestly confess, I didn't get you. The Muso's intent was to combine two bytes in a signed word (Int16). What do you mean by making the 'subtle' distinction between combining bytes/words and combining numbers? Can you please explain.

What is a byte? The industry has eventually agreed that's it's an eight-bit "octet", and that a word is sixteen bits, but those aren't necessarily numbers.

If you want to concatenate two bytes into a word, with absolutely no interpretation, then you use a variant record and are careful about endianness.

If you want to assemble 16 bits in memory from two 8-bit quantities then you use a shift and an "or" operation.

If you want to merge two numbers of a certain "bitedness" into one with a larger "bitedness", then you use multiplication and addition. But you'd better be damned sure of the representation convention, and be prepared to take sign into consideration.

Now, if we go back to @Muso's OP. "Two bytes"... but are those signed or unsigned? A byte isn't (necesarliy) a number. "Into a signed word"... but that's unambiguously a number.

So it really boils down to what one knows about the source values, and what one requires from the result.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### y.ivanov

• Full Member
• Posts: 125
##### Re: How to create a signed integer from bytes
« Reply #19 on: April 16, 2021, 01:20:22 am »
MarkMLl,

Now I get it. Thank you for your explanation for something that, I believe, is quite obvious for both of us.
I don't think the Muso's question goes so abstract and deep, and I would recommend him to find some good book for computer data representation, binary systems, processor architectures, etc.

What is a byte? The industry has eventually agreed that's it's an eight-bit "octet", and that a word is sixteen bits, but those aren't necessarily numbers.
Actually, they are whole numbers in a specific range. At a certain level of abstraction, you can consider them as a domain of bijection to another (finite) set, e.g. characters, enums, floats, etc.

If you want to concatenate two bytes into a word, with absolutely no interpretation, then you use a variant record and are careful about endianness.
Concatenation means there is interpretation. That is why the endianness is important.

If you want to assemble 16 bits in memory from two 8-bit quantities then you use a shift and an "or" operation.
You are utmost correct. But since we know well that the addition (the adder) is an "or" combined with a "xor" for the carry-bit to the next cell, and also, the SHL operation (same instruction) fills with zeroes on the right, we're quite sure that "+" works the same way as the "or" and there will be no carry-bit from the low byte.

If you want to merge two numbers of a certain "bitedness" into one with a larger "bitedness", then you use multiplication and addition. But you'd better be damned sure of the representation convention, and be prepared to take sign into consideration.
Quite correct again. But we also know that SHL/SHR are ignoring the signednness of the item, which is not relevant in the case when we're working with unsigned variables, and then they behaves just like multiplication/division by the power of two. This is our particular case (variables of type 'byte').

Now, if we go back to @Muso's OP. "Two bytes"... but are those signed or unsigned? A byte isn't (necesarliy) a number. "Into a signed word"... but that's unambiguously a number.

So it really boils down to what one knows about the source values, and what one requires from the result.
IMO, The Muso wants to transfer some signed words through a communication link, but I can't be absolutely sure from his writings.

And yes, the one must knows what heis doing, especially when trying to combine some low level artifacts, plays with shifts, bytes and so.

Regards,

#### speter

• Full Member
• Posts: 210
##### Re: [solved] How to create a signed integer from bytes
« Reply #20 on: April 16, 2021, 02:31:05 am »
Muso,

To achieve what (I tihnk) you are after, use (something like):
Code: Pascal  [Select][+][-]
1. var
2.   b1 : byte = 200;
3.   b2 : byte = 255;
4.   w : word;
5.   i : int16;
6. begin
7.   w := b1 shl 8 + b2;
8.
9.   if w > maxsmallint then
10.     i := -int16(w and maxsmallint)
11.   else
12.     i := int16(w);
13.
14.   memo1.append('w='+w.tostring+'; i='+i.tostring);
15. end;

The above gives me:
Code: [Select]
`w=51455; i=-18687`
Note that "maxsmallint" may be OS (etc) dependant; and of course my example assumes there is a memo.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills.

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

#### egsuh

• Hero Member
• Posts: 740
##### Re: [solved] How to create a signed integer from bytes
« Reply #21 on: April 16, 2021, 04:32:18 am »
This is tagged as "Solved" but I found this subject is interesting.  Following code shows -1, which I thing is corrent. Right?

procedure TForm1.Button1Click(Sender: TObject);
var
b1, b2: byte;
i: int16;
begin
b1:= \$FF;
b2:= \$FF;

i:= b1 * \$100 + b2;
showmessage(IntToStr(i));
end;