richardoc
asked on
Join 2 words to make a dword
I am reading data back from a microcontroller using modbus. I need to join two words together to make a dword.
I have both word values stored in variables in C, as below:
var1 = 15
var2 = 25
I need to join them so var3 has the value 1525
I also need to be able to split them back up into 2 words.
Are there standard C functions for performing these operations? cast 2 words to a dword?
I have both word values stored in variables in C, as below:
var1 = 15
var2 = 25
I need to join them so var3 has the value 1525
I also need to be able to split them back up into 2 words.
Are there standard C functions for performing these operations? cast 2 words to a dword?
ASKER
Basically I have 2 integers, and I need to join them to make a long . I used 15 and 25 just as examples, but the end value must be <value1><value2>
Does this make sense?
Does this make sense?
How About:
int num1=15,num2=25;
long longnum=(num1*100)+num2;
int num1=15,num2=25;
long longnum=(num1*100)+num2;
>I also need to be able to split them back up into 2 words.
int num1,num2;
num1=longnum%100;
num2=longnum/100;
int num1,num2;
num1=longnum%100;
num2=longnum/100;
Oh My God,WHat am i typing?
I need some sleep.
I need some sleep.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
small bug [notice right shift]:
To split them once again:
var1= var3>>16;
var2= var3&0xFFFF;
To split them once again:
var1= var3>>16;
var2= var3&0xFFFF;
I might need some sleep as well... :(
Your question is ambiguous.
Most computers nowdays are binary base, so when you put two bytes together, you end up with 256 times the high byte plus the low byte.
From your example, it looks like you expect to get 100 times the high part plus the low part.
Please clarify, do you want to combine the digits decimally, or binary-wise?
Regards,
grg99
Most computers nowdays are binary base, so when you put two bytes together, you end up with 256 times the high byte plus the low byte.
From your example, it looks like you expect to get 100 times the high part plus the low part.
Please clarify, do you want to combine the digits decimally, or binary-wise?
Regards,
grg99
Your question is also rather schizo. You seem to know well about words and dword (assembler stuff) but seems not to be familiar with more or less elementary bit-fiddling operators or assembler. If my solution above is what you were after. Or am I mistaken?? Or is this a trick question?
ASKER
I'm a high level programmer working with assembly programmers, so I know some terms but don't neccessarily understand them :) Bad I know, but hey, you gotta start somehow. sjef_bosman's answer did the job nicely. Thanks all.
var3= (var1<<8)|var2
Other operations are comparable (but always bitwise).