Hello there, I've encountered a syntax in one of the examples I'm studying.

*pStatus = *pStatus | DC_ST_DISK_IN | DC_ST_TRAYCLOSE | DC_ST_WRITING;  //<--notice the "|"

Is this a form of piping? Like in bash script in Linux which supports piping in there commands? uhmmm.. What does it imply, or what is its purpose? Thanks

Recommended Answers

All 14 Replies

Its bitwise OR operator. Read this.

Oh, I see. So these variables (DC_ST_DISK_IN, DC_ST_TRAYCLOSE, DC_ST_WRITING) are bit flags. By the way, their values are in the form of this one (0x0001, 0x0002 etc) <- is this binary? how would they be compared with each other using the "|" bitwise operator? Thank you.

>how would they be compared with each other using the "|" bitwise operator?
Look at it this way: each flag uses a single bit to represent its value, something similar to this:
00000001 = 1
00000010 = 2
00000100 = 4
etc.

The bitwise OR compares each bit, and if either of them are set, the resulting bit is set. So if you were to xor 1 and 4, you would end up with
00000101 = 5

Using the result and the bitwise AND, another function can determine which flags are set, because each bit represents a flag.

Check out this thread also might be helpful for you..

Thanks, I'm still having a problem understanding this example program I'm examining (actually it's a dvd-driver controller). But I now understand how these flags and the operators work. Thank you again!

The code you posted is:
- pStatus represents multiple flags. Each flag is a binary fag (can have value 0 or 1).
- to set these flags you OR them (using or operator "|") with some values. If you examine the other thread I posted you'll see how the flags are set.

If you had simple boolean variable to represent these flags you would have 3 stmts to set them to true. Here you're optimizing it in 1 stmt.

One last question. How would you know the certain flag of a variable. Like for example, I have here two variables of type int.

int a = 9, b = 3;

I will swap the contents of the variables using the XOR(^) operator.

a ^= b;
b ^= a;
a ^= b;

How did the swapping happened? Are these variables, once initialized, may have a default flag? Or, the swapping were done by manipulating the memory location using the XOR(^) bitwise operator? Thank you.

Try using a dry run method..
Convert the variable values in binary and then use the XOR oerator on them.

U 'll be surprized to see the results..

Try using a dry run method..
Convert the variable values in binary and then use the XOR oerator on them.

U 'll be surprized to see the results..

I've already tried that before I placed this post. I'm just curious on how the compiler manipulates the swapping of variables. What is it's basis? The memory location of the entire variable? lol...Funny me.

> What is it's basis?
Its simple mathematics, no complications of memory location involved. The XORing takes care of all that. See this.

But like Salem has already pointed out, XOR swap is the thing of the past not drawing out and 'ooh' or 'aah' in present day scenarios.

I was just wondering how these bitwise operators work given only the variables. If it manipulates the memory address. Thanks again.

>>If it manipulates the memory address
No. It doesn't manipulate the memory address, it manipulates the contents of memory (at that address).

Ahhh...Thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.