How do you use HighByte()/LowByte()

I was going through the reference trying to find ways to make my code work easier, and I stumbled upon high and low byte (), The reference doesn't ahve examples of what they do. How do they work? and How can they be used?

I would suspect something like this:

int test = 0xAB; 
byte hi, lo; 

hi = HighByte(test);  //in case you're wondering this should be 0xA
lo = LowByte(test);    // and this should be 0xB.

So all they do is read the high and low byte? Does this mean you can't set the high or low byte/ write to a high or low byte with them?

Uh, those are nybbles, not bytes

bubulindo: I would suspect something like this:

int test = 0xAB; 
byte hi, lo; 

hi = HighByte(test);  //in case you're wondering this should be 0xA lo = LowByte(test);    // and this should be 0xB.

The example should be

int test = 0xABCD;   // 16 bits
byte hi, lo;  // 8 bits each

hi = HighByte(test);  //in case you're wondering this should be 0xAB
lo = LowByte(test);    // and this should be 0xCD.
1 Like

Here is an example where I needed to send a 16 bit int value to a I2C device that uses 16 bit register, but the Wire I2C library only supports byte transfers so used:

//send high threshold compair value word to device
  Wire.beginTransmission(deviceAdd);  // transmit to ADS1115 device
  Wire.send(0x03);                           // point to high threshold register
  Wire.send(highByte(hiCompair));      // sends MSB of high threshold word
  Wire.send(lowByte(hiCompair));       // sends LSB of high threshold word
  Wire.endTransmission();                 // stop transmitting
1 Like

knightschade: So all they do is read the high and low byte? Does this mean you can't set the high or low byte/ write to a high or low byte with them?

What do you want to do?

Bytes can be set by bit operations or using a union. It would help me to decide what to explain if you tell me what you want to do.

In short I have an LCD screen being controlled by an i2c device. The way the pinning worked out, most of the 7 segment sections are spanning two registers. I have tried the following:

1) I made separate functions to write the necessary bits to form my numbers on the LCD

// 1 => BC
void one() {
  bitClear(BankB,7);
  bitClear(BankB,6);
  bitClear(BankB,5);
  bitClear(BankB,4);
  bitSet(BankC,1);
  bitSet(BankC,3);
  bitClear(BankC,2);

This works but uses a lot of code, and I wasn't sure how to get a temp sensor reading to display if all of my numbers were functions like that.

2) following some other examples either posted here or seen on the web I made an array of bytes to form the numbers for the display:

byte Ones[10] = {
   B11110000, //BankC 0,6,8
   B00000101, //BankD 0,1,7
   B11010000, //BankC 2
   B00000110, //BankD 2
   B10010000, //BankC 3
   B00000111, //BankD 3,4,8,9
   B00100000, //BankC 4
   B10110000, //BankC 5,9
   B00000011, //BankD 5,6
   B00010000, //BankC 7
};

Than I started messing around with "and" "or" and "xor" to try and display my numbers, but found that depending on what the prior number was and what the number would be, sometimes the numbers combined ( ie going from "zero" to "two" actually displayed an "eight") as the and/or/xor would not cancel the last number out properly and artifacts were left over filling the entire byte ( B11111111).

So I was scouring the reference, and saw high and low byte, but didn't understand what that did, so I was wondering how it worked. To possibly find a way to write my code for my display.

Read my reply to your other post to see the solution (in short, reset everything to 0x00 before ORing data in)

highbyte and lowbyte are macros for these actions (and return bytes, NOT nibbles!):

#define highByte(x) ( (x) >> (8) ) // keep upper 8 bits
#define lowByte(x) ( (x) & (0xff) ) // keep lower 8 bits

Theres a great use of highByte() and lowByte() in this example. It partitions a unsigned int into two bytes to send it to two shift registers

http://arduino.cc/en/Tutorial/ShftOut13

I use it on my most recent matrix, its very useful actually

I would suspect something like this:

int test = 0xAB; byte hi, lo;

hi = HighByte(test); //in case you're wondering this should be 0xA lo = LowByte(test); // and this should be 0xB.

Why "suspect", when it is clear from the reference that your suspicions are unfounded? http://arduino.cc/en/Reference/LowByte http://arduino.cc/en/Reference/HighByte

OK Thanks I think I got it.