Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc
From: Eric Sosman (eric.sosman_at_sun.com)
Date: 01/12/05
- Next message: Mike Wahler: "Re: Parse "Card * const wDeck""
- Previous message: Alan Balmer: "Re: re-entrant function????????"
- In reply to: no spam: "Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Next in thread: Lawrence Kirby: "Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Reply: Lawrence Kirby: "Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Reply: bd: "Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 12 Jan 2005 16:36:33 -0500
no spam wrote:
> What is the implementation independent way of moving the least significant
> byte of unsigned char to the most significant byte of unsigned int?
#include <limits.h>
unsigned char uc = ...;
unsigned int ui;
/* Almost right: works on every machine I've ever
* run across, but is not actually guaranteed by
* the Standard
*/
ui = (unsigned int)uc << (CHAR_BIT * (sizeof ui - 1));
/* Best "completely portable" solution I've thought of;
* allows for padding bits in unsigned int
*/
ui = uc;
for (unsigned int n = UINT_MAX >> CHAR_BIT; n > 0; --n)
ui <<= 1;
Maybe there's a way to calculate (UINT_MAX + 1)/(UCHAR_MAX + 1)
without risking zero in the numerator and/or denominator, but I
haven't figured one out. (Note that UCHAR_MAX == ULLONG_MAX is
permitted by the Standard.)
> And the least significant word (if not a word, have the preprocessor force
> an error) of unsigned int to the most significant word of unsigned long?
What is a "word?" The C Standard uses the term mostly to
refer to its own content, twice to refer to "words in a line
of text," and once in connection with floating-point numbers;
it is never used in connection with an unsigned int.
-- Eric.Sosman@sun.com
- Next message: Mike Wahler: "Re: Parse "Card * const wDeck""
- Previous message: Alan Balmer: "Re: re-entrant function????????"
- In reply to: no spam: "Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Next in thread: Lawrence Kirby: "Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Reply: Lawrence Kirby: "Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Reply: bd: "Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|