Searching \ for '[PIC]: Best way to get lower and higher bytes of a' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Best way to get lower and higher bytes of a'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Best way to get lower and higher bytes of a'
2002\03\13@061820 by Carlos Ojea

flavicon
face
Hello :

I have a 'int' variable called 'i_eeprom' and I want to get its lower and
higher byte.
This is my code:

int i_eeprom;
char EEADR_L;
char EEADR_H;
EEADR_L = (char) (i_eeprom & 0x00FF);
EEADR_H = (char) ((i_eeprom / 256) & 0x00FF);

And it works, but I think there is a better way to do this.
Any thoughts?

Thanks,
Carlos

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@062647 by Bond, Peter

flavicon
face
> EEADR_H = (char) ((i_eeprom / 256) & 0x00FF);
>
> And it works, but I think there is a better way to do this.
> Any thoughts?

I personally would use >>8 instead of /256, but the compiler will *probably*
evaluate it to the same thing.  The subsequent mask is superfluous.

Peter
This email, its content and any attachments is PRIVATE AND CONFIDENTIAL to
TANDBERG Television. If received in error please notify the sender and
destroy the original message and attachments.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@063308 by michael brown

flavicon
face
> int i_eeprom;
> char EEADR_L;
> char EEADR_H;
> EEADR_L = (char) (i_eeprom & 0x00FF);
> EEADR_H = (char) ((i_eeprom / 256) & 0x00FF);
>
> And it works, but I think there is a better way to do this.
> Any thoughts?

unsigned int i_eeprom;
unsigned char EEADR_H, EEADR_L;
EEADR_L = (unsigned char) (i_eeprom & 0xFF)
EEADR_H = (unsigned char) (i_eeprom >> 8);

I don't know if this is "better", but the EEADR_H assignment should generate
less code.

michael brown

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@063724 by michael brown

flavicon
face
>The subsequent mask is superfluous.

I don't think so, as he was using signed elements.  e.g. 0xFFFF >> 8 =
0xFFFF  Although the assignment should truncate the upper 8 bits.

michael brown

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@073327 by Francisco Ares

flavicon
face
The best way, IMHO, is the following:

union Int2Chars
{
   unsigned int ints;
   char bytes[2];
};

union Int2Chars i_eeprom;

void main(void)
{
...
i_eeprom.ints = 30000;    // integer number

WriteEEPROM (EEPROMaddress, i_eeprom.chars[1]);    // fake write
routine, sending MSB of integer
WriteEEPROM (EEPROMaddress+1, i_eeprom.chars[0]);    // fake write
routine, sending LSB of integer
...

}

This, of course, leads to more typing and more attention, but also less
assembly code.

Francisco


Carlos Ojea wrote:

{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@093806 by Bond, Peter

flavicon
face
> I don't think so, as he was using signed elements.  e.g. 0xFFFF >> 8 =
> 0xFFFF  Although the assignment should truncate the upper 8 bits.

Fair point - reading in skim mode again.

given E1>>E2
"The right shift is equivalent to division by 2^E2 if E1 is unsigned or it
has a non-negative value; otherwise the result is implementation defined"
K&R

On conversions - again, for *unsigned* types, the assignment and implicit
conversion should truncate the left part to fit.  For signed, it is
implementation defined again.

I don't use signed variables when I'm working with bytes & bitfields!  (but
then, I don't remember when I last declared anything as a float, either)

Peter


This email, its content and any attachments is PRIVATE AND CONFIDENTIAL to
TANDBERG Television. If received in error please notify the sender and
destroy the original message and attachments.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@105742 by Carlos Ojea

flavicon
face
Now I have:
 EEADR_L = (unsigned char) (i_eeprom & 0xFF);
 EEADR_H = (unsigned char) (i_eeprom >> 8);
And I also define i_eeprom as 'unsigned int'.
EEADR_L and EEADR_H as 'unsigned char'.
Thank you all !!.Regards,
Carlos

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@170906 by Spehro Pefhany

picon face
At 04:51 PM 3/13/02 +0100, you wrote:
>Now I have:
>   EEADR_L = (unsigned char) (i_eeprom & 0xFF);
>   EEADR_H = (unsigned char) (i_eeprom >> 8);
>And I also define i_eeprom as 'unsigned int'.
>EEADR_L and EEADR_H as 'unsigned char'.

Incidentally, I get exactly the same number of words of code emitted
with ANY of the suggested methods, when optimization is cranked up.
Portability should be the first concern, and let the compiler worry
about *how* to do it (unless it is a stupid compiler, then all bets are off)

Unions look like they would produce faster code, but in fact they do not
with a smart compiler. Similarly, the compiler should get rid of the
(probably unneeded) and-ing with 0xFF above.

It's important (not in this case, but in general) to remember that >>
has implementation-defined behavior with signed numbers, according to the
standard.

Best regards,

Spehro Pefhany --"it's the network..."            "The Journey is the reward"
spam_OUTspeffTakeThisOuTspaminterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
9/11 United we Stand

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@181235 by Mike Mansheim

flavicon
face
> I have a 'int' variable called 'i_eeprom' and I want to get its lower
> and higher byte.

It's not standard C, but one of CCS C's "extensions" is really slick for
doing this (just to confuse things, an "int" is 8 bits and a "long" is
16 bits in CCS C):

unsigned long i_eeprom;
#byte     EEADR_L = i_eeprom
#byte EEADR_H = i_eeprom + 1

and no shifting or dividing by 256 is required!

I know this violates the "portability is paramount" requirement, but it
sure is handy when making portable code is not required.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\13@183944 by David Koski

flavicon
face
Maybe something like this?

int i_eeprom;
char EEADR_L;
char EEADR_H;
EEADR_L = ((char*)(&i_eeprom))[0];
EEADR_H = ((char*)(&i_eeprom))[1];

Overhead is at compile time instead of runtime.

David

On Wed, 13 Mar 2002 12:12:13 +0100
Carlos Ojea <.....carlosojeaKILLspamspam@spam@LEVELTELECOM.ES> wrote:

{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...