"Lie detector" code with hexadecimal numbers, >>, & etc

Hi!
I do not understand this code. can anybody help me please?

It is the lie detector code of Simon Monk. I do not understand why write red, green and blue as a hexadecimal number which is (Ive calculated: 0xFF0000=16711680, 0x00FF00 = 65280, 0x000080 = 128;(Maybe I wrong).

int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int buzzerPin = 7;

int potPin = A1;
int sensorPin = A0;

long red = 0xFF0000;//Numero hexadecimal que significa: //0*1+0*16+0*256+0*4096+15*65536+15*1048576=16711680
long green = 0x00FF00;//Numero hexadecimal que significa: //0*1+0*16+15*256+15*4096+0*65536+0*1048576=65280
long blue = 0x000080;//Numero hexadecimal que significa: //0*1+8*16+0*256+0*4096+0*65536+0*1048576=128

int band = 10;

void setup()
{
  pinMode (potPin, INPUT);
  pinMode (sensorPin, INPUT);
  pinMode (redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode (bluePin, OUTPUT);
  pinMode (buzzerPin, OUTPUT);
}

void loop()
{
  int gsr = analogRead(sensorPin);
  int pot = analogRead(potPin);
  if (gsr > pot + band)
  {
    setColor(red);
    beep();
  }
  else if (gsr < pot - band)
  {
  setColor(blue);
  }
  else
  {
  setColor(green);
  }
}
  
 void setColor(long rgb)
  {
    int red = rgb >> 16;
    int green = (rgb >> 8) & 0xFF;
    int blue = rgb & 0xFF;
    analogWrite(redPin, 255-red);
    analogWrite(greenPin, 255-green);
    analogWrite(bluePin, 255-blue);
  }
void beep()
  {
    for (int i = 0; i< 1000; i++)
    {
    digitalWrite(buzzerPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(buzzerPin, LOW);
    delayMicroseconds(100);
    }
  }

The part that i understand less is:

  int red = rgb >> 16;
    int green = (rgb >> 8) & 0xFF;
    int blue = rgb & 0xFF;

Thank you very much

All this code is doing is setting each of these variables to the corresponding color. I'm not sure why he doesn't use 255 (0xFF) for blue instead of 128 (0x80) but they are both colors of blue.

long red = 0xFF0000;//Numero hexadecimal que significa: //0*1+0*16+0*256+0*4096+15*65536+15*1048576=16711680
long green = 0x00FF00;//Numero hexadecimal que significa: //0*1+0*16+15*256+15*4096+0*65536+0*1048576=65280
long blue = 0x000080;//Numero hexadecimal que significa: //0*1+8*16+0*256+0*4096+0*65536+0*1048576=128

RGB color specification is 3 bytes representing RGB. The following code is just isolating the individual color bytes in order to output to the individual LED outputs.

int red = rgb >> 16;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;

This is something you will need to learn. It is used very often. All computers have a fixed number of bits used for computation. You will have heard of 8bit, 16bit, 32bit ... computers.

Many times, we do not need all the bits to represent something. Just like with decimal numbers, you are just used to ignore digits before the largest digit of your number e.g. 000123 is 123 but we never write the leading 0.

So instead of ignoring the leading bits we do not need, often bits are combined. Have a look into a datasheet of any microcontroller. The registers often have little groups of bits combined, even single bits.

For instance, bit [0] might be a ON/OFF bit, bit [1:2] are 4 possible values for a function select and so on. But they are all stored in one memory address.

The same is true for color in a 32-bit machine. If you have 8-bit colors you can have 4 channels for instance red, blue, green, alpha(transparency). Or you can have 3 channels with 8 bits and ignore 8 bits.

The >> shifts bits right and << shifts bits left

0x00FF0000 >> 16 = 0x000000FF

By shifting the numbers, you can unpack the individual numbers. But you need to "delete" the bits you don’t want. That what the bitwise AND (&&) is used for. For red, in the code, this happens automatically, everything leaving on the right gets thrown away. But if the number had an alpha channel the code would be wrong. You would need

int red = (rgb >> 16) && 0xFF;

Ok thank very much Klaus_K and ToddL1962.

So what means then?:

long red = 0xFF0000;
long green = 0x00FF00;
long blue = 0x000080;

and:

int red = rgb >> 16;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;

I mean waht decimal number can use instead of those hexas?

Thank you very much again

You can convert the numbers with a calculator app or even with Google e.g. "0xFF00 to decimal" or "0xFF00 to binary"

But additionally, I recommend you read up on hexadecimal and binary notation and Two's complement.

Two's complement

These are the basics for numbers in digital computers.

The conversion from Google is true for unsigned numbers only. Signed numbers are stored in twos complement, because this will allow negative numbers and still works with the same digital logic for addition/subtraction in the CPU.

The nice thing about hexadecimal is that it can be easily converted to binary. Each symbol in hex represents exactly 4 bits in binary. So, a 3-symbol hex number is a 12-bit binary number. This does not work with the decimal system. 10 in decimal is A.

long red = 0xFFAA80;

Converting the number of a RGB value into decimal has little meaning. Each color is a separate bundle of 8 bits. You cannot do normal calculations on those numbers. You must do each color channel separately. Red 0-255(0x00 - 0xFF) Green 0-255 Blue 0-255.

There are special instructions called SIMD that can calculate these kind of data (Audio, graphics, ...).

Esch hexadecimal digit stands in place for exactly 4 binary digits.

0000 = 0
1000 = 8
1111 = F

If you use decimal numbers then they don't line up perfectly:

0000 = 0
1000 = 8
1111 = 15

You can also use Octal, where each digit represents 3 binary digits. That is rarer because then an 8 bit byte is two-and-a-half octal digits.

When you are working with RGB colors like this, it is much easier to use the hexadecimal digits because the 24 binary digits are too long to keep track of them. You can quickly look to see which number to change if you want to add a little more red or a little more green.

Thank you very much MorganS and Klaus_K.
This is new for me and I do not understand very well at the moment, but I´ll try to think on it and I hope understand little by little.
Many thanks to you and this Forum. It is a very wonderful can learn fron others. I´m crazy for learn and understan.
Merry Christmas everybody