gviaud 0 Report post Posted December 17, 2003 I would like to know how to extract the two 8bit sub-components from a 16 bit variable and put them in an 8bit variable. psudocode example: psudo() { short X16 = 52428; char Lower8 = 0; char Upper8 = 0; Lower8 = GetLower(X16); Upper8 = GetUpper(X16); } Upper8 and Lower8 should now equal 204... How do I make "GetLower" and "GetUpper" with available operand? The reason I ask is, I am having trouble with some 16Bit code and think there may be a bug but can not debug it without being able to actually "see" what the values are... Thanks, George Quote Share this post Link to post Share on other sites
DAC 0 Report post Posted December 19, 2003 You can shift the bits 8 places like this. ////////////////////////////////////////////////// // From one 16bit variable two 8bit variables int Var16bit = 0xBCAC; // 0xBCAC == 48300 char Var8bit1; char Var8bit2; //Because Var8bit1 is an 8bit variable only 0xAC is left over. Var8bit1 = Var16bit; // AC == 172 // Shift the 8 bits 8 places (0x00BC) Var8bit2 = Var16bit>>8; // BC == 188 ////////////////////////////////////////////////// // From two 8bit variable one 16bit variable Var8bit1 = 0xAC; // AC == 172 Var8bit2 = 0xBC; // BC == 188 Var16bit = 0; Var16bit = Var8bit2*256+Var8bit1; // 0xBCAC == 48300 Regards, David Quote Share this post Link to post Share on other sites
Guest ysj Report post Posted December 20, 2003 try these codes this will take up less memory #include short X16@0x20; char Lower8@0x20;char Upper8@0x21; main(){ char UpperX16, LowerX16; for (; { X16 = 52428; LowerX16 = Lower8; UpperX16 = Upper8; X16 = 0xa3b4; LowerX16 = Lower8; UpperX16 = Upper8; } } Quote Share this post Link to post Share on other sites
Guest Pavel Report post Posted December 22, 2003 Here is another solution from similar topic from the old forum: I just create a global variable and access it using assembly like this: int Value; char bHi; asm movf _Value+1, W asm movwf _bHi Now the high bite of Value is in bHi. I make these variables global because it makes it easier to name them since the name is just the variable name with an underscore. Regards, Pavel Quote Share this post Link to post Share on other sites