DS1822: How does the program assign the sensor's index?

Just wondering... How does the program assign the sensor's index in sensor.getTempCByIndex()?

I will be using this code:

// Load Libraries for DS1820 and OneWire
#include <OneWire.h>
#include <DallasTemperature.h>
 
// Variables for temperature readings
float tempOne;
float tempTwo;
 
// DS1820 Data wire is plugged into pin 4 on the Arduino
#define ONE_WIRE_BUS 3
 
// Setup oneWire instance to communicate with devices
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
 
void setup()
{
   Serial.begin(9600);
   // Start the OneWire library
   sensors.begin();
}
 
void loop()
{
 
  // Read the temperature
  readtemp();
  // Write the Results to the serial Monitor
  serialPrint();
 
}
 
void readtemp()
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Send the command to get temperatures
  tempOne = (sensors.getTempCByIndex(0));
  tempTwo = (sensors.getTempCByIndex(1));
 
}
 
void serialPrint()
{
Serial.print("Sensor1: ");
Serial.print(tempOne);
Serial.print("C");
Serial.print("  Sensor2: ");
Serial.print(tempTwo);
Serial.println("C");
delay (500);
}

How will I know which sensor will be assigned to 0 and 1?
Will it be based on its address? or just whatever I connected first?

The OneWire bus software will enumerate the devices in numerical order. However, the numerical order isn't quite what you'd expect. For example when I run a sketch to identify two DS18B20s it finds them in this order:

ROM = 28 D4 C9  0  4  0  0 49
ROM = 28 1E BD  0  4  0  0 6B

Looks like it has got them reversed but when it enumerates them, it checks each byte of the address low order bit first.
The two addresses obviously differ in the second byte. Writing the bits out and then reversing them:

   normal    reversed 
D4=11010100  00101011=2B
1E=00011110  01111000=78

When the address is rewritten with the bits in the order that the enumeration protocol does it, you can see that what we write as 'd4' is seen as '2B' by the enumeration software.

So, the answer is that the devices are indexed in numerical order of address with the low order bits of each address byte checked first.

Pete