Interpretation of integer input by read() function

How will Serial.read() function read an integer value(say 1100) in Arduino?
"int" takes up 2 bytes of memory and read function reads only 1 byte at a time.

For following piece of code

unsigned int servopos=bluetooth.read();   //reads 1byte of data
unsigned int servopos1=bluetooth.read();       //reads 2nd byte
    //unsigned int realservo=servopos1+servopos; this (statement1) gives ambiguous output
unsigned int realservo=servopos1*256+servopos;    //I want to understand this line (statement2)

Here,"bluetooth" is an object of SoftwareSerial function,it reads up data from a bluetooth module which sends integer data between 1000 and 1180,
if suppose data is sent as value 1100 I want to receive the same data value 1100,
but I can't get that without using 2nd statement!
How does multiplying 256 to 2nd byte of received data solves problem?

The hint you want is "ASCII codes".

Or Robin2's serial basics topic.

@op

1. Your source data is: 1100 = 0x044C (2-byte binary coded)

2. You are receiving the original data via Bluetooth (an UART device) as two bytes: 1st 4C and then 04. The data bytes get stored in FIFO type buffer.

3. The original data (0x044C = 1100) could be reconstructed in the following ways:

int servopos = bluetooth.read();  //lower byte

sm65.png
Figure-1: Lower byte view in memory

int servopos1 = bluetooth.read(); //higher byte

sm66x.png
Figure-2: Upper byte view in memory

4. Let us combine Fig-1 and Fig-2 together to get 1100 = 0x044C.
Shift 0x04 of Fig-2 into its upper 8-bit positions (bit-8 to bit15) and put 0x00 into lower 8-bit positions (bit-0 to bit7). After doing this, we will have the following picture.
sm67.png
Figure-3:

5. Now, place Fig-1 on the top of Fig-3 and combine them using OR function. We will get : 0x044C = 1100 = the original data.
sm68.png
Figure-4:

6. Operation of Step-4 is carried out this way using program instructions:

int x1 = servopos1 << 8;

The operation is known as arithmetic shift to the left; in this process, the lower 8-bit shifts to the left by 8-bit positions and 8 zeros (0s) automatically enter into lower 8-bit positions.

After shifting process, we have got:
x1 = 0x0400 = 1024.

The above value can also be obtained by multiplying 0x04 by 256 :
0x04 * 256 = 1024.
==> 0x04 * 28 = 1024

7. Now, let us find the value of the variable realservo.

int realservo = (servopos1 <<8)|servopos;
==> realservo = (servopos1*256)|servopos;
==> realservo = servopos1*256 + servopso;  //arithmetic OR (|) is = arithmetic sum (+)

Hope, the point is clear?

sm68.png

sm65.png

sm66.png

sm67.png

sm66x.png

sm65.png

GolamMostafa:
@op

1. Your source data is: 1100 = 0x044C (2-byte binary coded)

More likely, the source data is 0x31 0x31 0x30 0x30

AWOL:
More likely, the source data is 0x31 0x31 0x30 0x30

I agree that it is more likely ASCII Coded data; however, it could also be binary coded data without any harm.

I have taken the 'binary coded data' as example having thought that it would be easier to explain to OP the interpretative meaning of 'realservo = servopos1*256 + servopos', which he has sought.

GolamMostafa:
@op

1. Your source data is: 1100 = 0x044C (2-byte binary coded)

2. You are receiving the original data via Bluetooth (an UART device) as two bytes: 1st 4C and then 04. The data bytes get stored in FIFO type buffer.

3. The original data (0x044C = 1100) could be reconstructed in the following ways:

int servopos = bluetooth.read();  //lower byte

sm65.png
Figure-1: Lower byte view in memory

int servopos1 = bluetooth.read(); //higher byte

sm66x.png
Figure-2: Upper byte view in memory

4. Let us combine Fig-1 and Fig-2 together to get 1100 = 0x044C.
Shift 0x04 of Fig-2 into its upper 8-bit positions (bit-8 to bit15) and put 0x00 into lower 8-bit positions (bit-0 to bit7). After doing this, we will have the following picture.
sm67.png
Figure-3:

5. Now, place Fig-1 on the top of Fig-3 and combine them using OR function. We will get : 0x044C = 1100 = the original data.
sm68.png
Figure-4:

6. Operation of Step-4 is carried out this way using program instructions:

int x1 = servopos1 << 8;

The operation is known as arithmetic shift to the left; in this process, the lower 8-bit shifts to the left by 8-bit positions and 8 zeros (0s) automatically enter into lower 8-bit positions.

After shifting process, we have got:
x1 = 0x0400 = 1024.

The above value can also be obtained by multiplying 0x04 by 256 :
0x04 * 256 = 1024.
==> 0x04 * 28 = 1024

7. Now, let us find the value of the variable realservo.

int realservo = (servopos1 <<8)|servopos;

==> realservo = (servopos1256)|servopos;
==> realservo = servopos1
256 + servopso;  //arithmetic OR (|) is = arithmetic sum (+)




Hope, the point is clear?

@GolamMostafa Sir,I am a novice so I tried hard to understand your answer and googled but I couldn't understand it,can you please provide any link about binary data/system which could be understood by beginners.

but I couldn't understand it

Don't worry about it. Most of the times, I don't understand his/her point, and most of the times the point is completely irrelevant.

@OP

This is what you submitted in your original post:

How will Serial.read() function read an integer value(say 1100) in Arduino?
"int" takes up 2 bytes of memory and read function reads only 1 byte at a time.

Here,"bluetooth" is an object of SoftwareSerial function,it reads up data from a bluetooth module which sends integer data between 1000 and 1180,
if suppose data is sent as value 1100 I want to receive the same data value 1100,
but I can't get that without using 2nd statement!
How does multiplying 256 to 2nd byte of received data solves problem?

1. 1100 (0x044C) has come into your receiver (UNO) as two bytes -- 04 and 4C; where 4C arrived first and then 04 arrived. (Please, don't ask -- why?)

2. The data bytes of Step-1 have entered into a FIFO (First-in First-out) type storage area called buffer (should I say that it is ring type buffer?). 4C (01001100) has arrived first; so, it has occupied upper location. 04 (00000100) has arrived later; so, it has occupied the next lower location. Now, when we perform read operation on the FIFO buffer, the data that (4C) entered first will come out first.

3. Now, you understand that the the original data was an int (16-bit) and now we have two separate 8-bit chunk (2-byte).

4. We need two read operations to bring the data bytes from the FIFO and then combine them to get '16-bit one data item'.

byte x1 = Serial.read();   //x1 holds = 4C
byte x2 = Serial.read();  //x2 holds = 04

5. Now, can you wok on x1 and x2 to make them 16-bit and put into 16-bit variable x?
(1) Transform 04 ----> to 0400
(2) Transform 4C -----> to 004C
(3) add 0400 and 004C -----> 044C
(4) Execute this instruction: Serial.print(0x044C, DEC); // display shows: 1100

6. Coding of the Pseudo Codes of Step-5.

unsigned int y1 = (unsigned int)x1 <<8;   //y1 = 0400

or 

unsigned int y1 = (unsigned int) x1*256;    //y1 = 0400

unsigned int y2 = (unsigned int)x1;           //y2 = 004C

unsigned int x = y1 + y2;   //x = 0x044C;

Serial.print(x, DEC); //show: 1100

PaulS:
Don't worry about it. Most of the times, I don't understand his/her point, and most of the times the point is completely irrelevant.

But we take time to understand your wits enjoy your humour.

Now, can you wok on x1 and x2 to make them 16-bit and put into 16-bit variable x?
(1) Transform 04 ----> to 0400
(2) Transform 4C -----> to 004C
(3) add 0400 and 004C -----> 044C
(4) Execute this instruction: Serial.print(0x044C, DEC); // display shows: 1100

Thank you @GolamMostafa sir,
I finally understood the binary and hexadecimal number representations in memory,hope I will understand the internal working of FIFO and ring type buffer in upcoming weeks.

achal1:
How will Serial.read() function read an integer value(say 1100) in Arduino?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R