Can not figure out why '-1' is recognized as 'int'


GS3.cpp:104:106: error: narrowing conversion of '-1' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]

static const uint8_t pin2int[23] = {-1, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 2, 3};


Wondering that how to edit and help the coding to read '-1' here? For me, I do not understand why '-1' is read as a '16-bit', (which refer to '-' and '1'?). How should I modify the code to read

'{-1, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 2, 3}'. Thanks

Integer literals are seen as an int. -1 is a integer literal.

And what value do you expect to store a -1 in an unsigned datatype :wink:

static const uint8_t pin2int[23] = {-1, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 2, 3};

You know the u in uint8_t stands for unsigned right? You know what unsigned means right?

Leesh:

GS3.cpp:104:106: error: narrowing conversion of '-1' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]

This is saying that when something like "uint8_t var = -1;" gets compiled, it will be FORCED to interpret the line as "uint8_t var = 255". This is bad enough that the compiler raises a fatal error and your code can't finish compiling.

Basically, when you assign a negative value to an unsigned variable, you will get integer overflow. Also, look up two's complement.

You have the number -1.

You want to store it in a memory location (of ATmega328P of UNO Board), which is 8-bit.

We have to follow one of the following two methods to store -1 into the said memory location:
Signed Magnitude:
-1 ==> 10000001
MSBit indicates sign (here 1 refers to -); lower 7-bit indicate magnitude (1) ==> -1

Two's ~~Compliment:~~Complement Edit
-1==>11111111
MSBit indicates negative positional value (-1x27 = -128), and the lower 7-bit carrie positive positional value (1x26+, ..., +1x20 = 127) ==> -128+127==> -1

ATmega328P MCU supports Two's Compliment Complement Method. So, we will decide to store 11111111 for -1.

We are using Arduino Platform and the associated Compiler. What data type should we use so that the variable/number is treated as -1(negative number) during arithmetic operation? The answer is: int8_t or char.

The valid declaration is:

int8_t x = 0b11111111;
Serial.print(x, DEC); //shows: -1

or

char x = 0b11111111;
Serial.print(x, DEC); //shows: -1

GolamMostafa:
You have the number -1.

You want to store it in a memory location (of ATmega328P of UNO Board), which is 8-bit.

We have to follow one of the following two methods to store -1 into the said memory location:
Signed Magnitude:
-1 ==> 10000001
MSBit indicates sign (here 1 refers to -); lower 7-bit indicate magnitude (1) ==> -1

Two's Compliment:
-1==>11111111
MSBit indicates negative positional value (-1x27 = -128), and the lower 7-bit carries positive positional values (1x26+, ..., +1x20 = 127) == -1

ATmega328P MCU supports Two's Compliment Method. So, we will decide to store 11111111 for -1.

We are using Arduino Platform and the associated Compiler. What data type should we use so that the variable/number is tread as -1 during arithmetic operation? The answer is: int8_t or char.

The valid declaration is:

int8_t x = 0b11111111;

Serial.print(x, DEC); //shows: -1




or



char x = 0b11111111;
Serial.print(x, DEC); //shows: -1

sp. "Two's complement"