# Using 16Bit variables

## Recommended Posts

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

##### Share on other sites

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

##### Share on other sites

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;

}

}

##### Share on other sites

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

## Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.