How to split an integer into two hex/binary values for two registers?

Thread Starter

z_iron

Joined May 24, 2020
17
the integer long variable 'count' is dependent on a function parameter 'time'. I want to set the two registers TMR0H and TMR0L to this variable however since this a 16-bit register pair, how do i split the variable, count, into two 8-bit values where TMR0H is obviously larger in terms of the bit position (TMR0H 65536 : 256, TMR0L 128: 1)?

Code:
    unsigned long count = ((1000000)/128)*(time/1000);
    TMR0H = "HIGHER BITS OF COUNT";
    TMR0L = "LOWER BITS OF COUNT";
 

dl324

Joined Mar 30, 2015
11,486
Code:
TMR0H = (count & 0xFF00) >> 8;
TMR0L = (count & 0x00FF);
EDIT: Your description is confusing. You talk about a 16 bit register pair, but you want to store 8 bit values in them. If the integer is 16 bits and the registers are 8 bits, the above code will work. If not, modify the code fragment accordingly.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
6,795
Code:
TMR0H = (count & 0xFF00) >> 8;
TMR0L = (count & 0x00FF);
EDIT: Your description is confusing. You talk about a 16 bit register pair, but you want to store 8 bit values in them. If the integer is 16 bits and the registers are 8 bits, the above code will work. If not, modify the code fragment accordingly.
The shift operands “lose” the bits shifted past the end of the value. So
TMR0H = (count & 0xFF00) >> 8​
is the same as
TMR0H = count >> 8​
This fact will save you an operation.
 

WBahn

Joined Mar 31, 2012
26,143
Code:
TMR0H = (count & 0xFF00) >> 8;
TMR0L = (count & 0x00FF);
EDIT: Your description is confusing. You talk about a 16 bit register pair, but you want to store 8 bit values in them. If the integer is 16 bits and the registers are 8 bits, the above code will work. If not, modify the code fragment accordingly.
May not even need to do that. If TMR0x are unsigned, you can probably just do

TMR0H = count >> 0;
TMR0L = count;

I prefer the masking because it makes what is being done explicit, but if you NEED performance, these might give you some.
 

Thread Starter

z_iron

Joined May 24, 2020
17
The shift operands “lose” the bits shifted past the end of the value. So
TMR0H = (count & 0xFF00) >> 8​
is the same as
TMR0H = count >> 8​
This fact will save you an operation.
Thanks for the reply. Could you explain how bit shifting by 8 works?
 

jpanhalt

Joined Jan 18, 2008
10,221
In Assembly, the assembler does it for you.
Code:
movlw     high(.1058)
Disassembles to:
Code:
0000    3004     MOVLW 0x4                      3:          movlw high(.1058)
No extra steps or much thought required. I am a little surprised C compilers don't offer something simple like that.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
6,795
You have a 16 bit value in count. Shifting right one bit drops the rightmost bit, and the remaining bits move one position to the right. A zero is inserted on the left.

Example. You have the value
1010 1010 1000 0110​
in a 16 bit variable. Shifting right one position results in the following value
0101 0101 0100 0011​
Spaces are inserted to make it easier to read.

If you start with the same value as before, and shift right eight positions, you get the value
0000 0000 1010 1010​

This is the value of the high byte of the original 16 bit value.
 
Top