typecasting a bitfield to unsigned long long

Discussion in 'C Programming' started by Sushil, Nov 28, 2003.

  1. Sushil

    Sushil Guest

    hi Gurus

    I'm a newbie learning C.
    I've a question, relevant code snippet is as follows :

    typedef unsigned long long ulonglong;

    struct foo {
    unsigned dummy : 2;
    unsigned n : 24;
    unsigned c : 3;
    unsigned d : 1;
    unsigned v : 1;
    unsigned g : 1;
    };

    ulonglong func_foo(void) {
    ulonglong addr;
    struct foo entry;

    /* code that populates entry snipped*/

    addr = entry.n << 12;
    ..
    ..
    }

    This caused sign extended value in addr which is not we want.
    Would the following be a good fix or clc gurus have a better suggestion?

    addr = ((ulonglong)entry.n) << 12;

    I'm concerned that there is more to it than what I feel the fix is.

    Thanks in advance
    - Sushil
     
    Sushil, Nov 28, 2003
    #1
    1. Advertisements

  2. Sushil

    Jack Klein Guest

    This is a problem, due to C's value preserving promotion rules.
    Assuming ints have more than 24 bits on your platform, the unsigned
    value in entry.n is converted to a signed int, then left shifted 12
    times producing a signed int value. If your ints have 32 bits, the
    top four bits of entry.n are shifted off the end and disappear.
    This will work just fine. The 24 bits in entry.n will be placed in
    the lowest 24 bits of an unsigned long long. No sign extension will
    occur, and there is no possibility of overflow.
    There are ways to define a union consisting of a bit-mapped structure
    and an unsigned long long, but doing it with the cast is more portable
    and doesn't need to be modified if any of the bit-fields change size,
    position, or are added or deleted.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
     
    Jack Klein, Nov 28, 2003
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.