Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bitwise-inclusive-OR operator

0 views
Skip to first unread message

Kulsoom

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
Hello,

I wanted to know if these cna be strung together like so:

servers = servers | SERVER1 | SERVER2 | SERVER3 |SERVER4;

Examples I see only do two variables: servers=servers|SERVER1;
--
comp.lang.c.moderated - moderation address: cl...@plethora.net

Michiel Salters

unread,
Apr 20, 2000, 3:00:00 AM4/20/00
to
Kulsoom wrote:

> Hello,

> I wanted to know if these cna be strung together like so:

> servers = servers | SERVER1 | SERVER2 | SERVER3 |SERVER4;

> Examples I see only do two variables: servers=servers|SERVER1;

Yes, this works and actually is common.

I assume SERVER1 etc. are all of a suitable type such that
servers |= SERVERx is valid. In that case, the above is equal
to

servers = (((( servers | SERVER1 ) | SERVER2 ) | SERVER3 ) | SERVER4)

The operator "|" takes two expressions, and joins them into
one expression. Any of the two expressions it takes may be
of the form A|B itself.

HTH,
Michiel Salters

Insh_Allah

unread,
Apr 20, 2000, 3:00:00 AM4/20/00
to
----- Original Message -----
From: "Kulsoom" <gte...@prism.gatech.edu>
Newsgroups: comp.lang.c.moderated
Sent: Wednesday, April 19, 2000 5:41 AM
Subject: bitwise-inclusive-OR operator


> Hello,
>
> I wanted to know if these cna be strung together like so:
>
> servers = servers | SERVER1 | SERVER2 | SERVER3 |SERVER4;
>
> Examples I see only do two variables: servers=servers|SERVER1;

Oh yes, you can.

To give you two examples where this sort of stuff is happening (as you'll
see it's often used to compose values that have particular bits or series of
bits set):

--------- ex1 ------------------
/* constants for parsing an SNMP packet, according to ASN.1 */

/* ASN.1 object types */
/* -- Tag class encoding -- */
#define ASN1BER_UNIVERSAL 0x00
#define ASN1BER_APPLICATION 0x40
#define ASN1BER_CONTEXTSPECIFIC 0x80
#define ASN1BER_PRIVATE 0xC0

/* -- Tag primitive/contructed form -- */
#define ASN1BER_PRIMITIVE 0x00
#define ASN1BER_CONSTRUCT 0x20

#define ASN1_SEQ ( ASN1BER_UNIVERSAL | ASN1BER_CONSTRUCT |
0x10 ) /* (48) sequence object */
#define ASN1_SET_OF ( ASN1BER_UNIVERSAL | ASN1BER_CONSTRUCT |
0x11 ) /* (49) 'Set Of xxx' object */
#define ASN1_INT ( ASN1BER_UNIVERSAL | ASN1BER_PRIMITIVE |
0x02 ) /* (2) 'Integer' */
#define ASN1_OCTSTR ( ASN1BER_UNIVERSAL | ASN1BER_PRIMITIVE |
0x04 ) /* (4) 'Octet String' */
#define ASN1_NULL ( ASN1BER_UNIVERSAL | ASN1BER_PRIMITIVE |
0x05 ) /* (5) 'Null' */
#define ASN1_OBJID ( ASN1BER_UNIVERSAL | ASN1BER_PRIMITIVE |
0x06 ) /* (6) 'Object Identifier' */
#define ASN1_IPADDR ( ASN1BER_APPLICATION | ASN1BER_PRIMITIVE |
0x00 ) /* (64) 'IpAddress' */
#define ASN1_COUNTER ( ASN1BER_APPLICATION | ASN1BER_PRIMITIVE |
0x01 ) /* (65) 'Counter32' */
#define ASN1_GAUGE ( ASN1BER_APPLICATION | ASN1BER_PRIMITIVE |
0x02 ) /* (66) 'Gauge32' */
#define ASN1_TIMETICKS ( ASN1BER_APPLICATION | ASN1BER_PRIMITIVE |
0x03 ) /* (67) 'TimeTicks' */
#define ASN1_OPAQUE ( ASN1BER_APPLICATION | ASN1BER_PRIMITIVE |
0x04 ) /* (68) 'Opaque' -- backward compatibility only! (RFC2578, section
7.1.9.: Opaque) */

/* ... etc ... */

/* ... snippet of code from the SNMP packet parse function ... */

/* read sequence header */
asn1_read(&obj, packp);
if (obj.type != ASN1_SEQ)
{
DIAG_ERROR(DIAG_SECTION_SNMP,"snparse: Must start with ASN1 Sequence!",
NOVAR, 0, 0);
return -1;
}

/* ... end of snippet ... */


------------ ex.2 -------------

/* function from a Hardware driver for a 3C509 network chip */

BOOLEAN el3_setmcast(PIFACE pi) /* __fn__ */
{
PEL3_SOFTC sc;
int ioaddr;

sc = iface_to_l3softc(pi);
if (!sc)
return(FALSE);

/* Set or clear the multicast filter for this adaptor.
(NOTE: n is always >= 0)
n == -1 Promiscuous mode, receive all packets
n == 0 Normal mode, clear multicast list
n > 0 Multicast mode, receive normal and MC packets. The
3c509 driver does not provide the capability to
filter multicast, so accept all multicast; if no
socket is listening on the address, it will be
dropped at the protocol interpret layer.
*/
ioaddr = sc->ia_iobase;
if (pi->mcast.lenmclist > 0)
{
outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast,
ioaddr + EL3_CMD);
}
else if (pi->mcast.lenmclist < 0)
{
outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
ioaddr + EL3_CMD);
}
else
outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);

return(TRUE);
}

--------- end of examples -------

Noe that the #define's have parentheses around the ORed values to make sure
the order of evaluation is always the same, no matter where the MACRO is
used.

Groetjes/Greetinx,

Ger

----------------------------------------------------------------------------
--
Ger Hobbelt a.k.a. Insh_Allah mailto:i...@wanadoo.nl
----------------------------------------------------------------------------
--
### I feel the Need, the Need for Speed!!! (Tom Cruise in Top Gun)
###
Thank God I, bein' a self-conscious hEmail guy, won't get a ticket for
speedin'
on the Internet Highway! And bein' a rather nice, cute, non-discriminative
kinda hEmail dude deep down underneath I can honestly say I brake for ANY
attractive shEmail crossin' my way. Not just the blonde ones (No! Sir!) the
others too as long as they're lookin' fine! Ain't that mighty swell o' mine?
... Yeah, right ...

Hermann Kremer

unread,
Apr 20, 2000, 3:00:00 AM4/20/00
to
Kulsoom schrieb in Nachricht ...

>Hello,
>
>I wanted to know if these cna be strung together like so:
>
>servers = servers | SERVER1 | SERVER2 | SERVER3 |SERVER4;
>
>Examples I see only do two variables: servers=servers|SERVER1;
>--

Yes, they can. I would recommend to put the whole expression
into parentheses

servers = (servers | SERVER1 ... );

since C has somewhat peculiar operator precedences.
You can also write

servers |= (SERVER1 | ...);

Hermann

Jerry Coffin

unread,
Apr 20, 2000, 3:00:00 AM4/20/00
to
In article <clcm-2000...@plethora.net>,
gte...@prism.gatech.edu says...

> Hello,
>
> I wanted to know if these cna be strung together like so:
>
> servers = servers | SERVER1 | SERVER2 | SERVER3 |SERVER4;

Sure. In this case, you can use:

servers |= SERVER1 | SERVER2 | SERVER3 | SERVER4;

--
Later,
Jerry.

The universe is a figment of its own imagination.

GaryHC

unread,
Apr 20, 2000, 3:00:00 AM4/20/00
to
Kulsoom gte...@prism.gatech.edu wrote:

>I wanted to know if these cna be strung together like so:
>
>servers = servers | SERVER1 | SERVER2 | SERVER3 |SERVER4;
>

>Examples I see only do two variables: servers=servers|SERVER1;

Yes, your example statement will produce the value you would expect.
(Whether that value is valid depends on how the software interprets it.)

-- Gary Culp

Barry Schwarz

unread,
Apr 23, 2000, 3:00:00 AM4/23/00
to
On 19 Apr 2000 03:41:42 GMT, Kulsoom <gte...@prism.gatech.edu> wrote:

>Hello,


>
>I wanted to know if these cna be strung together like so:
>
>servers = servers | SERVER1 | SERVER2 | SERVER3 |SERVER4;
>
>Examples I see only do two variables: servers=servers|SERVER1;

Yes.


<<Remove the del for email>>

0 new messages