SPLIT 16 BIT VAR IN 2 VAR OF ONE BYTE

HiTech had a great compiler for PSoC's M8C processor, but, then they were bought by Microchip. Subsequently the HiTech forums were taken off line. At the request of PSoCDeveloper members PSoCDeveloper has created this forum to allow HiTech users to support each other.

Moderator: ericb

SPLIT 16 BIT VAR IN 2 VAR OF ONE BYTE

Postby appavito » Thu May 27, 2010 3:53 am

hi i need to split a 16 bit variabile in 2 variabile of one byte on c language.. and viceversa.. the MSB in the first var and the LSB on the second var...

can hel me???
appavito
Cheese Cube
Cheese Cube
 
Posts: 47
Joined: Thu May 27, 2010 2:28 am

Re: SPLIT 16 BIT VAR IN 2 VAR OF ONE BYTE

Postby slandrum » Thu May 27, 2010 12:18 pm

Do you mean something like this?
Code: Select all
   short var;          // 16 bit variable
   char hi, lo;        // 8 bit variables
   hi = (var>>8)&255;  // set hi to the upper 8 bits of var
   lo = (var&255);     // set lo to the lower 8 bits of var
   var = (hi<<8) + lo; // set var to 16 bit value from hi and lo

Or do you mean something else?
slandrum
The Big Cheese
The Big Cheese
 
Posts: 453
Joined: Fri Feb 29, 2008 11:38 am
Location: California

Re: SPLIT 16 BIT VAR IN 2 VAR OF ONE BYTE

Postby IceCube » Sat May 29, 2010 4:51 am

Yo could do something like this:
Code: Select all
typedef union {
   unsigned int Word;
   struct {
      unsigned char Low;
      unsigned char High;
   } Byte;
} WordByteType;

WordByteType Value, AnotherValue;

Value. Word = 0x1234;
UART_PutChar(Value.Byte.Low);  //Send 0x34
UART_PutChar(Value.Byte.High);  //Send 0x12

AnotherValue.Byte.Low = 0x78;
AnotherValue.Byte.High = 0x65;
PWM16_WritePeriod(AnotherValue.Word);   // Write 0x6578

Regards,
Rolf
User avatar
IceCube
The Big Cheese
The Big Cheese
 
Posts: 198
Joined: Thu Apr 20, 2006 11:47 pm
Website: http://www.nooteboom.biz
Location: Helmond, The Netherlands

Re: SPLIT 16 BIT VAR IN 2 VAR OF ONE BYTE

Postby appavito » Tue Jun 01, 2010 3:38 am

good, but i not undestend something:

example var=1122; // 1122=0000010001100010 bynary
// hi =00000100 lo 01100010

hi = (var>>8)&255; // 0000000000000100 shift 8 position and insert 8 zero.
// typecast hi= var ( only the lsb ) hi= 00000100

BUT WHY USE &255 ????
appavito
Cheese Cube
Cheese Cube
 
Posts: 47
Joined: Thu May 27, 2010 2:28 am

Re: SPLIT 16 BIT VAR IN 2 VAR OF ONE BYTE

Postby slandrum » Wed Jun 02, 2010 10:26 am

Because if the number is negative, zeros won't get shifted into the upper bits, ones will.

A smart compiler will not generate any code for the &255, but it also prevents complaints for implied casting and out of range values if the number you are shifting is a constant.
slandrum
The Big Cheese
The Big Cheese
 
Posts: 453
Joined: Fri Feb 29, 2008 11:38 am
Location: California

Re: SPLIT 16 BIT VAR IN 2 VAR OF ONE BYTE

Postby lourencohen » Fri Oct 17, 2014 2:27 am

I am doing my IT course and we have started learning C language recently. As I am not an expert in C language, I am unable to fix this issue for you. But I am happy to see that the experts here are able to fix your issue and help you better.

high risk credit card processing
lourencohen
Newbie
Newbie
 
Posts: 2
Joined: Fri Jan 31, 2014 1:26 am


Return to “%s” HiTech C User-to-User Support

Who is online

Users browsing this forum: No registered users and 0 guests