2

I have a function getID() that returns a byte from the EEPROM:

byte getID(){
  return (byte)EEPROM.read(0x0199);
}

Later, (on line 65), I use this function and Bitwise-OR it with a binary value:

byte out[3] = {getID()|B10000000, B00000001, B00000001};
Serial.write(out,3);

and it returns a `narrowing conversion from 'int' to 'byte':

file.ino:65:33: warning: narrowing conversion of '(int)(((unsigned char)((int)getID())) | 128)' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]

     byte out[3] = {(getID())|B10000000, B00000001, B00000001};

It is just a warning and the code works fine (I think), but why is this a narrowing conversion? I've tried making sure just about everything is a byte...

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Cameron K
  • 398
  • 1
  • 9

1 Answers1

5

So I think I have found my answer then, thanks to some of the commenters.

The Bitwise-OR operation seems to return an int, regardless of what the original operands were. If I then take that returned value and cast it as a byte, it no longer gives the error:

byte out[3] = {(byte)((getID())|B10000000), B00000001, B00000001};

This is something I had not tried, thanks to the various commenters that lead me to learn this.

Cameron K
  • 398
  • 1
  • 9