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


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


Relevant Pages

  • Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc
    ... >> byte of unsigned char to the most significant byte of unsigned int? ... If you need to set the top byte in an existing unsigned int value ... >> And the least significant word (if not a word, ... > refer to its own content, twice to refer to "words in a line ...
    (comp.lang.c)
  • Re: repllog crashes CE5.0 when trying activesync
    ... The same usb function driver worked flawlessly in Wince4.2. ... REPLLOG!WndProc(HWND__ * 0x7c0139f0, unsigned int 0x00000600, unsigned ... table, hProcess = 0xF71DE35A ... Using '`USB Connection' connection ...
    (microsoft.public.windowsce.platbuilder)
  • Re: repllog crashes CE5.0 when trying activesync
    ... Just buy PB for CE 5.0, and you'll get 2 free support calls with your PB license key. ... REPLLOG!WndProc(HWND__ * 0x7c0139f0, unsigned int 0x00000600, unsigned ... table, hProcess = 0xF71DE35A ... Using '`USB Connection' connection ...
    (microsoft.public.windowsce.platbuilder)
  • Re: repllog crashes CE5.0 when trying activesync
    ... REPLLOG!WndProc(HWND__ * 0x7c0139f0, unsigned int 0x00000600, unsigned ... table, hProcess = 0xF71DE35A ... Using '`USB Connection' connection ...
    (microsoft.public.windowsce.platbuilder)
  • Re: repllog crashes CE5.0 when trying activesync
    ... REPLLOG!WndProc(HWND__ * 0x7c0139f0, unsigned int 0x00000600, unsigned ... table, hProcess = 0xF71DE35A ... Using '`USB Connection' connection ...
    (microsoft.public.windowsce.platbuilder)