Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
combine 3 hexvalues by using left shift operator.

examples:

take decimal values as 2,129025,127 .combine the all hex values and need to come as 2,1F801,7F

i need to write as 2,1F801,7F IN THE kvaser CANKING output.


What I have tried:

int h1=2;
int h2=129025;
int h3=127;
int combinedID = h1 + (h2<<8) + (h3<<25);

//for writing on kvaser canking
canlib.canwrite(handle,combinedID,data,data.length,0);
Posted
Comments
CPallini 22-Jan-24 2:53am    
And what's wrong with your code?
jeron1 yesterday    
Are there range restrictions on h1,h2,and h3?

:edit:
:sigh: I didn't realize how old this was, sorry.

1 solution

You need to shift by 28, not 25.

When shifting by hexadecimal digits your shifts will always need to be multiples of 4, as that is the size of a hexadecimal digit.

When you shift by 25 bits, you are trying to shift the 2 in the first number into the high bits of the 1 in the second number, resulting in 5F8017F instead of the expected 21F8017F
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900