data type int: 2 bytes or 4 ?

Hi,

does the 'int' data type consist of two bytes or 4?

code: int A = -1; Serial.println(A, BIN);

byte a = highByte(A); byte b = lowByte(A); Serial.println(a, BIN); Serial.println(b, BIN);

int B = word(a, b); Serial.println(B, BIN); Serial.println(B, DEC);


output:

A = -1 11111111111111111111111111111111 11111111 11111111 11111111111111111111111111111111 -1

A = 31000 111100100011000 1111001 11000 111100100011000 31000

A = -31000 11111111111111111000011011101000 10000110 11101000 11111111111111111000011011101000 -31000

The reference section gives the int as 2 bytes.

Also, how can the function word() reassemble 4 bytes? It takes only two times one byte as arguments. Right?

Again, the number of bytes is two for positive numbers and 4 for negative ones. Is this correct?

thanks for a clarification,

newbee

On the Arduino, 2. How can you have different numbers of bytes for positive and negative numbers?

Google 'twos complement'

does the ‘int’ data type consist of two bytes or 4?

The reference section gives the int as 2 bytes

The reference is correct.

The output of Serial.println() shows that there might be a casting to a long internally

Also, how can the function word() reassemble 4 bytes? I

It can’t. You can do it with the bitshift - http://arduino.cc/en/Reference/Bitshift - yourself:

// asign values for the bytes a,b,c,d
long x = a <<24 | b << 16 | c << 8 | d;

the number of bytes is two for positive numbers and 4 for negative ones. Is this correct?

No, again this is due to internal casting to a long.

Don't forget that leading 0's are suppressed when Serial printing stuff. The number of characters printed does not necessarily correspond to the bits in a type.