bitwise ORing

B

Bill Cunningham

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
 
L

Lew Pitcher

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?
 
J

James Kuyper

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.
 
D

Don Y

Hi Bill,

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...
 
B

Bill Cunningham

Lew said:
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
 
J

Joachim Schmitz

Don said:
Hi Bill,



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
 
B

Bill Cunningham

Lew said:
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
 
L

Lew Pitcher

Lew said:
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
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?
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with array 4
Functions 2
Implicit integer promotion 18
Trouble calling a function with enum parameter 3
JavaScript Challenge: Validating Email Addresses 1
Tasks 1
word size and gcc builtins usage 9
memset 24

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top