bitwise ORing

Discussion in 'C Programming' started by Bill Cunningham, Mar 23, 2012.

  1. What would a function prototype look like that takes one parameter but
    several options can be OR'd together in that parameter? For example:

    function(LEFT|RIGHT);

    What would the parameter source code look like?

    Bill
    Bill Cunningham, Mar 23, 2012
    #1
    1. Advertising

  2. Bill Cunningham

    Lew Pitcher Guest

    On Thursday 22 March 2012 21:56, in comp.lang.c, d wrote:

    > What would a function prototype look like that takes one parameter but
    > several options can be OR'd together in that parameter? For example:
    >
    > function(LEFT|RIGHT);
    >
    > What would the parameter source code look like?


    Well, Bill, that depends....

    What sort of argument does function() take?

    If you are writing function(), then you should know what sort of input you
    want it to take. OTOH, if function() has already been written, you can just
    look at it to determine the argument type.

    Given the little information that you've provided, we can eliminate some
    values. Because you already know that the argument will be the result of
    several values "Inclusively OR"ed together, you know that the argument must
    be an integer type (1999-9899 6.5.12 "Bitwise Inclusive OR operator" - each
    operand must be of integer type, and the usual arethmetic conversions will
    be performed).

    This means that the argument can't be a floatingpoint value (float, long
    float, double, long double, long long float), a pointer, a structure or a
    union. And that leaves char, unsigned char, short int, unsigned short int,
    int, unsigned int, long int, unsigned long int, long long int or unsigned
    long long int.

    Now, tell us, which one of those is it?
    --
    Lew Pitcher
    Lew Pitcher, Mar 23, 2012
    #2
    1. Advertising

  3. Bill Cunningham

    James Kuyper Guest

    On 03/23/2012 11:19 AM, Lew Pitcher wrote:
    > On Thursday 22 March 2012 21:56, in comp.lang.c, d wrote:
    >
    >> What would a function prototype look like that takes one parameter but
    >> several options can be OR'd together in that parameter? For example:
    >>
    >> function(LEFT|RIGHT);
    >>
    >> What would the parameter source code look like?

    >
    > Well, Bill, that depends....
    >
    > What sort of argument does function() take?
    >
    > If you are writing function(), then you should know what sort of input you
    > want it to take. OTOH, if function() has already been written, you can just
    > look at it to determine the argument type.
    >
    > Given the little information that you've provided, we can eliminate some
    > values. Because you already know that the argument will be the result of
    > several values "Inclusively OR"ed together, you know that the argument must
    > be an integer type (1999-9899 6.5.12 "Bitwise Inclusive OR operator" - each
    > operand must be of integer type, and the usual arethmetic conversions will
    > be performed).
    >
    > This means that the argument can't be a floatingpoint value (float, long
    > float, double, long double, long long float), a pointer, a structure or a
    > union. And that leaves char, unsigned char, short int, unsigned short int,
    > int, unsigned int, long int, unsigned long int, long long int or unsigned
    > long long int.


    Not quite - because of the integer promotions, the argument cannot be
    any type with an integer conversion rank lower than int.

    While what you've said is otherwise quite true for the argument of this
    function, the corresponding parameter type could be any type that the
    argument type can be implicitly converted to - which might be any
    arithmetic type, even _Bool.
    James Kuyper, Mar 23, 2012
    #3
  4. Bill Cunningham

    Joe keane Guest

    In article <4f6bca1d$0$32222$>,
    Bill Cunningham <> wrote:
    >What would the parameter source code look like?


    int function(struct foo *da_main_object, int some_other_important_value,
    unsigned int opts)
    {
    ...
    }
    Joe keane, Mar 23, 2012
    #4
  5. Bill Cunningham

    Don Y Guest

    Hi Bill,

    On 3/22/2012 6:56 PM, Bill Cunningham wrote:
    > What would a function prototype look like that takes one parameter but
    > several options can be OR'd together in that parameter? For example:
    >
    > function(LEFT|RIGHT);
    >
    > What would the parameter source code look like?


    Usually, this is an integer (of some size) data type.
    The manifest constants LEFT and RIGHT are typically disjoint
    bitsets (though they need not be). They can also be derived
    from an enumerated type, etc.

    E.g., render_text(const char *string, attr_t attributes)

    where:
    attributes ::= BLINK | BOLD | DIM | INVISIBLE | ITALIC | etc.

    (note my use of '|' here is in the context of a BNF, not a
    C statement; also, this specfication says nothing about how
    "sensible" BLINK | INVISIBLE might be! :> )

    So,
    render_text("Big SALE today!!", BOLD | BLINK);
    one of the semantic problems with the use of "or" is that it
    really is implementing what you would COLLOQUIALLY think of
    as an "and" function: "Make this BOLD *and* BLINKing".

    Unfortunately, using '+' as a more intuitive conjunctive
    opens the door for all sorts of "hidden" errors. E.g.,
    BLINK + BLINK might == BOLD, by coincidence. Or, overlapping
    bit patterns can cause other unexpected results. For example,
    BLINK + INVISIBLE == ITALIC (because the developer assumed
    "BLINK + INVISIBLE" was meaningless -- "/* CAN'T HAPPEN */ -- and
    chose the ITALIC bit pattern to coincide with this value,
    by chance.

    Of course, there are other perils with building values out
    of bit masks but that's another story...
    Don Y, Mar 23, 2012
    #5
  6. Lew Pitcher wrote:
    > Well, Bill, that depends....
    >
    > What sort of argument does function() take?
    >
    > If you are writing function(), then you should know what sort of
    > input you want it to take. OTOH, if function() has already been
    > written, you can just look at it to determine the argument type.
    >
    > Given the little information that you've provided, we can eliminate
    > some values. Because you already know that the argument will be the
    > result of several values "Inclusively OR"ed together, you know that
    > the argument must be an integer type (1999-9899 6.5.12 "Bitwise
    > Inclusive OR operator" - each operand must be of integer type, and
    > the usual arethmetic conversions will be performed).
    >
    > This means that the argument can't be a floatingpoint value (float,
    > long float, double, long double, long long float), a pointer, a
    > structure or a union. And that leaves char, unsigned char, short int,
    > unsigned short int, int, unsigned int, long int, unsigned long int,
    > long long int or unsigned long long int.
    >
    > Now, tell us, which one of those is it?


    I should've given an example of the function I've noticed takes this.
    Right off my head I can't remember it exactly but it has to do with unix
    socket programming. Some of the functions there take several OR'd together
    responses.

    Bill
    Bill Cunningham, Mar 23, 2012
    #6
  7. Don Y wrote:
    > Hi Bill,
    >
    > On 3/22/2012 6:56 PM, Bill Cunningham wrote:
    >> What would a function prototype look like that takes one
    >> parameter but several options can be OR'd together in that
    >> parameter? For example:
    >>
    >> function(LEFT|RIGHT);
    >>
    >> What would the parameter source code look like?

    >
    > Usually, this is an integer (of some size) data type.
    > The manifest constants LEFT and RIGHT are typically disjoint
    > bitsets (though they need not be). They can also be derived
    > from an enumerated type, etc.
    >
    > E.g., render_text(const char *string, attr_t attributes)
    >
    > where:
    > attributes ::= BLINK | BOLD | DIM | INVISIBLE | ITALIC | etc.
    >
    > (note my use of '|' here is in the context of a BNF, not a
    > C statement; also, this specfication says nothing about how
    > "sensible" BLINK | INVISIBLE might be! :> )
    >
    > So,
    > render_text("Big SALE today!!", BOLD | BLINK);
    > one of the semantic problems with the use of "or" is that it
    > really is implementing what you would COLLOQUIALLY think of
    > as an "and" function: "Make this BOLD *and* BLINKing".


    #define and | /* ;-) */
    ....
    render_text("Big SALE today!!", BOLD and BLINK);

    Bye, Jojo
    Joachim Schmitz, Mar 24, 2012
    #7
  8. Bill Cunningham

    Geoff Guest

    On Sat, 24 Mar 2012 11:29:16 +0100, "Joachim Schmitz"
    <> wrote:

    >#define and | /* ;-) */
    >...
    >render_text("Big SALE today!!", BOLD and BLINK);


    Ouch, ouch, ouch! But cool.
    Geoff, Mar 25, 2012
    #8
  9. Lew Pitcher wrote:

    > Well, Bill, that depends....
    >
    > What sort of argument does function() take?
    >
    > If you are writing function(), then you should know what sort of
    > input you want it to take. OTOH, if function() has already been
    > written, you can just look at it to determine the argument type.
    >
    > Given the little information that you've provided, we can eliminate
    > some values. Because you already know that the argument will be the
    > result of several values "Inclusively OR"ed together, you know that
    > the argument must be an integer type (1999-9899 6.5.12 "Bitwise
    > Inclusive OR operator" - each operand must be of integer type, and
    > the usual arethmetic conversions will be performed).
    >
    > This means that the argument can't be a floatingpoint value (float,
    > long float, double, long double, long long float), a pointer, a
    > structure or a union. And that leaves char, unsigned char, short int,
    > unsigned short int, int, unsigned int, long int, unsigned long int,
    > long long int or unsigned long long int.
    >
    > Now, tell us, which one of those is it?


    msgget with it's second parameter takes ORing. It just returns an int.
    As to whether or not structs or unions are involved I don't know. man 2
    msgget.

    Bill
    Bill Cunningham, Mar 25, 2012
    #9
  10. Bill Cunningham

    Lew Pitcher Guest

    On Sunday 25 March 2012 14:02, in comp.lang.c, d wrote:

    > Lew Pitcher wrote:
    >
    >> Well, Bill, that depends....
    >>
    >> What sort of argument does function() take?

    [snip]
    >> Now, tell us, which one of those is it?

    >
    > msgget with it's second parameter takes ORing. It just returns an int.
    > As to whether or not structs or unions are involved I don't know. man 2
    > msgget.


    OK, Bill

    I think that you answered your own question, then.

    You originally asked
    >>> What would a function prototype look like that takes one parameter but
    >>> several options can be OR'd together in that parameter?


    And now you say that you have the documentation for the function in
    question. That documentation clearly provides the function prototype

    : NAME
    : msgget - get a message queue identifier
    :
    : SYNOPSIS
    : #include <sys/types.h>
    : #include <sys/ipc.h>
    : #include <sys/msg.h>
    :
    : int msgget(key_t key, int msgflg);
    /
    See? That is the function prototype.

    OK?
    --
    Lew Pitcher
    Lew Pitcher, Mar 25, 2012
    #10
    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. rajan
    Replies:
    2
    Views:
    1,790
    dutchgoldtony
    Mar 9, 2005
  2. dohnut
    Replies:
    0
    Views:
    543
    dohnut
    Oct 20, 2003
  3. dohnut
    Replies:
    1
    Views:
    585
    Sam Holden
    Oct 21, 2003
  4. dohnut
    Replies:
    0
    Views:
    581
    dohnut
    Oct 21, 2003
  5. James Aguilar

    Oring multiple values

    James Aguilar, Feb 1, 2005, in forum: C++
    Replies:
    11
    Views:
    1,350
    Victor Bazarov
    Feb 3, 2005
Loading...

Share This Page