Attributes

Discussion in 'Java' started by switch, Aug 21, 2003.

  1. switch

    switch Guest

    I've seen code where people pass in 'attributes' by using the | operator.
    Like MULTI | TRANSPARENT | EDITABLE

    I want to use this kind of thing in my own code, and I know you need to set
    up each constant to be 1,2,4,8,16, etc, but my question is, given a value,
    how do I test if an attribute is set ?
    switch, Aug 21, 2003
    #1
    1. Advertising

  2. switch

    Jacob Guest

    switch wrote:
    > I've seen code where people pass in 'attributes' by using the | operator.
    > Like MULTI | TRANSPARENT | EDITABLE
    >
    > I want to use this kind of thing in my own code, and I know you need to set
    > up each constant to be 1,2,4,8,16, etc, but my question is, given a value,
    > how do I test if an attribute is set ?


    Use the binary and operator:

    if (attribute & TRANSPARENT)
    // TRANSPARENT is set
    etc.
    Jacob, Aug 21, 2003
    #2
    1. Advertising

  3. switch

    switch Guest


    > switch <> scribbled the following:
    >> I've seen code where people pass in 'attributes' by using the |
    >> operator.
    >> Like MULTI | TRANSPARENT | EDITABLE


    >> I want to use this kind of thing in my own code, and I know you need
    >> to set up each constant to be 1,2,4,8,16, etc, but my question is,
    >> given a value, how do I test if an attribute is set ?


    > Like this:


    > int attributes = getAttributesFromAMagicalPlace();
    > if ((attributes & MULTI) != 0) {
    > doMultiStuff();
    > }
    > if ((attributes & TRANSPARENT) != 0) {
    > doTransparentStuff();
    > }
    > if ((attributes & EDITABLE) != 0) {
    > doEditableStuff();
    > }


    Thank you Sir.
    switch, Aug 21, 2003
    #3
  4. switch <> scribbled the following:
    > Thank you Sir.


    I am pleased you figured out my real gender so quickly. Most people who
    don't know me initially assume I'm a woman (which I'm not, despite my
    feminine-sounding name).

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "We sorcerers don't like to eat our words, so to say."
    - Sparrowhawk
    Joona I Palaste, Aug 21, 2003
    #4
  5. switch

    Roedy Green Guest

    On Thu, 21 Aug 2003 14:15:46 +0200, Jacob <> wrote or
    quoted :

    >if (attribute & TRANSPARENT)


    In forth that would be :
    attribute TRANSPARENT AND IF

    in C that would be :
    if (attribute & TRANSPARENT)

    but in Java it has to be :
    if ( (attribute & TRANSPARENT) != 0 )

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Aug 21, 2003
    #5
  6. switch

    Roedy Green Guest

    On Sat, 23 Aug 2003 11:05:13 -0700, "Jon A. Cruz" <>
    wrote or quoted :

    >Then it'd have to be
    >
    >if ( (attribute & TRANSPARENT) == TRANSPARENT )


    I did some work JavaDocing and cleaning a large Java project. The
    thing I found more difficult to deal with than anything else was
    deciding which enumeration masks were ints, which where single bit
    mask and which where multiple bit masks and checking they were all
    being used consistently.

    Pascal with its sets and automatic enum numbering kept track. It
    seems odd that Java can't do at least as well. The typical Pascal
    implementation puts a fixed size or a cap on set size, which would
    unlikely be considered acceptable in Java.

    If we are not going to give any language support for bit map enums,
    perhaps Sun could at least offer some naming conventions to help
    clarify intentions.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Aug 23, 2003
    #6
    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. Max
    Replies:
    1
    Views:
    478
    Joe Kesselman
    Sep 22, 2006
  2. P4trykx
    Replies:
    2
    Views:
    1,807
    bruce barker
    Jan 31, 2007
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    370
    Bruno Desthuilliers
    Jun 20, 2007
  4. Kyle Schmitt
    Replies:
    3
    Views:
    188
    Kyle Schmitt
    Jul 24, 2007
  5. Jayden
    Replies:
    16
    Views:
    514
    Steven D'Aprano
    Sep 29, 2012
Loading...

Share This Page