Unsigned Int and Variable at address

Discussion in 'C Programming' started by John T., Jun 25, 2003.

  1. John T.

    John T. Guest

    Hi NG.
    I have two questions.
    First, could I address some bit of an unsigned integer? lets say the integer
    "mrtest" holds the value 0xFFFF0000 and I would like to do something like
    (this is wirtten in some pseudo code)
    if(mrtest(bit7,bit6,bit5,bit4 and bit 3 == 0b00000)
    {
    do a lot of things;
    }


    Second, how do I store a variable at a specific address?
    for example
    Store mrtest at address 0xFFFF1166.

    Best Regards
    John
     
    John T., Jun 25, 2003
    #1
    1. Advertising

  2. John T.

    Dan Pop Guest

    In <bdc3br$ppq$-c.dk> "John T." <> writes:

    >First, could I address some bit of an unsigned integer? lets say the integer
    >"mrtest" holds the value 0xFFFF0000 and I would like to do something like
    >(this is wirtten in some pseudo code)
    >if(mrtest(bit7,bit6,bit5,bit4 and bit 3 == 0b00000)
    >{
    >do a lot of things;
    >}


    Try to use some kind of pseudo code that makes sense to other people, too.
    If you want to test that all these bits are 0, build an unsigned mask
    containing ones only in the corresponding positions: 0xF8U should do the
    job (assumming bit0 is the least significant one). A bitwise and between
    the value to be tested and the mask will tell you if all the relevant
    bits are zero:

    if ((mrtest & 0xF8U) == 0) {
    /* do a lot of things */ ;
    }

    The extra pair of parentheses is needed because the & operator has the
    "wrong" precedence WRT the == operator.

    >Second, how do I store a variable at a specific address?
    >for example
    >Store mrtest at address 0xFFFF1166.


    This is a very unwise thing to do, unless you have a *good* reason for
    doing it. Most modern platforms use virtual memory, which makes such
    an exercise pointless.

    The answer is to define mrtest as a macro:

    #define mrtest (*(unsigned int *)0xFFFF1166)

    Now, the identifier mrtest will give you an unsigned int stored at address
    0xFFFF1166, assuming that this address is suitable for storing an unsigned
    int at all. If it isn't, your program may crash at the first attempt to
    use mrtest...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 25, 2003
    #2
    1. Advertising

  3. John T.

    dbtid Guest

    On Wed, 25 Jun 2003 14:09:34 +0200, John T. <> wrote:

    > Hi NG.
    > I have two questions.
    > First, could I address some bit of an unsigned integer? lets say the
    > integer
    > "mrtest" holds the value 0xFFFF0000 and I would like to do something like
    > (this is wirtten in some pseudo code)
    > if(mrtest(bit7,bit6,bit5,bit4 and bit 3 == 0b00000)
    > {
    > do a lot of things;
    > }
    >
    >


    I'm not quite sure I understand what you're asking, but I'll state some
    assumptions and you can go from there...

    I assume by 0b00000 you mean binary 5 bits worth of 0's.

    If that's the case, then, yes, this is possible, and even better, it's
    easy.

    if ((mrtest & 0x000000fc) == 0)
    {
    // do whatever.
    }

    If any of the bits in mrtest[7:3] is a 1, the result of the bitwise and
    will be non-zero.


    > Second, how do I store a variable at a specific address?
    > for example
    > Store mrtest at address 0xFFFF1166.
    >


    Declare a pointer of the appropriate type and set it to the address you
    want.
    Then assign the value through that pointer.

    a) more traditionally:

    unsigned int *xFFFF1166 = (unsigned int *) 0xFFFF1166;
    *xFFFF1166 = mrtest;

    b) other way:

    *((unsigned int *) 0xFFFF1166) = mrtest;

    HTH
     
    dbtid, Jun 25, 2003
    #3
  4. John T.

    Dan Pop Guest

    In <> dbtid <> writes:

    >I assume by 0b00000 you mean binary 5 bits worth of 0's.
    >
    >If that's the case, then, yes, this is possible, and even better, it's
    >easy.
    >
    > if ((mrtest & 0x000000fc) == 0)
    > {
    > // do whatever.
    > }
    >
    >If any of the bits in mrtest[7:3] is a 1, the result of the bitwise and
    >will be non-zero.


    Nope, your mask contains *six* set bits (0xC == 8 + 4), so the result
    can be non-zero even if *all* the five "target" bits are zero.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 25, 2003
    #4
  5. John T.

    dbtid Guest

    On 25 Jun 2003 13:21:55 GMT, Dan Pop <> wrote:

    > In <> dbtid <dbtid@dev-
    > null.us> writes:
    >
    >> I assume by 0b00000 you mean binary 5 bits worth of 0's.
    >>
    >> If that's the case, then, yes, this is possible, and even better, it's
    >> easy.
    >>
    >> if ((mrtest & 0x000000fc) == 0)
    >> {
    >> // do whatever.
    >> }
    >>
    >> If any of the bits in mrtest[7:3] is a 1, the result of the bitwise and
    >> will be non-zero.

    >
    > Nope, your mask contains *six* set bits (0xC == 8 + 4), so the result
    > can be non-zero even if *all* the five "target" bits are zero.
    >
    > Dan


    To quote Homer Simpson, "Doh!"

    Thanks, Dan, for pointing that out.

    To the OP: sorry to have slightly incorrectly answered your question.

    The bitmask ought to be 0x000000f8.

    Humble apologies.
     
    dbtid, Jun 25, 2003
    #5
  6. Emmanuel Delahaye, Jun 25, 2003
    #6
  7. John T.

    CBFalconer Guest

    Dan Pop wrote:
    > Emmanuel Delahaye <> writes:
    >

    .... snip ...
    > >
    > > If it's a hardware address, I'll add 'volatile'
    > >
    > > #define mrtest (*(unsigned volatile int *)0xFFFF1166)

    >
    > It's supposed to be a memory address and last time I checked,
    > memory counted as hardware. Yet, values stored in memory are
    > not supposed to change by themselves, therefore volatile is not,
    > a priori, justified.


    The mere fact that you are defining such a location carries a
    strong implication that something else knows about that location,
    and thus may be modifying it. The only other reasonable
    interpretation I see is that it participates in memory-mapped i/o,
    in which case the volatile is again needed to prevent optimizing
    out repeated output events.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jun 26, 2003
    #7
  8. John T.

    Dan Pop Guest

    In <> CBFalconer <> writes:

    >Dan Pop wrote:
    >> Emmanuel Delahaye <> writes:
    >>

    >... snip ...
    >> >
    >> > If it's a hardware address, I'll add 'volatile'
    >> >
    >> > #define mrtest (*(unsigned volatile int *)0xFFFF1166)

    >>
    >> It's supposed to be a memory address and last time I checked,
    >> memory counted as hardware. Yet, values stored in memory are
    >> not supposed to change by themselves, therefore volatile is not,
    >> a priori, justified.

    >
    >The mere fact that you are defining such a location carries a
    >strong implication that something else knows about that location,
    >and thus may be modifying it. The only other reasonable
    >interpretation I see is that it participates in memory-mapped i/o,
    >in which case the volatile is again needed to prevent optimizing
    >out repeated output events.


    Your imagination is limited (but, at least, you haven't left your brain in
    "neutral" this time ;-) Suppose that you have two types of memory
    (e.g. slow/fast or cached/uncached or volatile/backed up) and you
    want to force this particular object to be allocated on a certain
    type of memory, hence the explicit address.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 26, 2003
    #8
  9. John T.

    CBFalconer Guest

    Dan Pop wrote:
    > CBFalconer <> writes:
    > >Dan Pop wrote:
    > >> Emmanuel Delahaye <> writes:
    > >>

    > >... snip ...
    > >> >
    > >> > If it's a hardware address, I'll add 'volatile'
    > >> >
    > >> > #define mrtest (*(unsigned volatile int *)0xFFFF1166)
    > >>
    > >> It's supposed to be a memory address and last time I checked,
    > >> memory counted as hardware. Yet, values stored in memory are
    > >> not supposed to change by themselves, therefore volatile is not,
    > >> a priori, justified.

    > >
    > >The mere fact that you are defining such a location carries a
    > >strong implication that something else knows about that location,
    > >and thus may be modifying it. The only other reasonable
    > >interpretation I see is that it participates in memory-mapped i/o,
    > >in which case the volatile is again needed to prevent optimizing
    > >out repeated output events.

    >
    > Your imagination is limited (but, at least, you haven't left your
    > brain in "neutral" this time ;-) Suppose that you have two types
    > of memory (e.g. slow/fast or cached/uncached or volatile/backed up)
    > and you want to force this particular object to be allocated on a
    > certain type of memory, hence the explicit address.


    That smells highly of 'kluge'. Such a job is better left to the
    linker/loader or whatever you want to call that phase of
    executable creation. Of course the language limitations of a
    particular implementation may force you to such a dirty kluge.
    There certainly is no standard way of handling it, including the
    original pegged address.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jun 26, 2003
    #9
  10. John T.

    manirajashirwad

    Joined:
    Sep 1, 2008
    Messages:
    2
    what does *int a; mean in C

    what does it mean and where will it be used
    *int a;
     
    manirajashirwad, Sep 3, 2008
    #10
  11. John T.

    manirajashirwad

    Joined:
    Sep 1, 2008
    Messages:
    2
    how to define a integer variable as pointer and pointer as ainteger variable
     
    manirajashirwad, Sep 3, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Freiberger
    Replies:
    3
    Views:
    998
    Bob Hairgrove
    Oct 30, 2004
  2. Schnoffos
    Replies:
    2
    Views:
    1,257
    Martien Verbruggen
    Jun 27, 2003
  3. er
    Replies:
    6
    Views:
    529
    Andre Kostur
    Sep 14, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    440
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    796
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page