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

Dani AI

Generated

Quick summary: the | in that example is the C/C++ bitwise OR operator, not a shell-style pipe. Constants written like 0x0001 use hexadecimal notation; they’re chosen so each constant has exactly one bit set (1, 2, 4, 8 ...). That lets multiple independent boolean flags be packed into a single integer. and laid out the basic idea; also pointed out why this is more compact than separate booleans.

Common, safe patterns and helpers:

enum Flags : unsigned {
  FlagDisk  = 1u << 0,
  FlagTray  = 1u << 1,
  FlagWrite = 1u << 2,
};

unsigned status = 0;
status |= FlagDisk | FlagWrite;   // set bits
if ((status & FlagTray) != 0) { /* tray flag set */ }
status &= ~FlagWrite;             // clear a flag
status ^= FlagDisk;               // toggle a flag

inline bool is_set(unsigned s, unsigned f) { return (s & f) != 0; }
inline void set_flag(unsigned &s, unsigned f) { s |= f; }
inline void clear_flag(unsigned &s, unsigned f) { s &= ~f; }

Important cautions and tips: always use parentheses when testing masked values (for example, if ((status & FLAG) == FLAG)), because == binds differently and will produce wrong results otherwise. Use unsigned types and 1u << n when defining flags to avoid signed-shift issues; shifting by a value >= width of the type is undefined. If the flag variable is shared between threads, updates must be atomic (or protected by a mutex) — otherwise use std::atomic or atomic fetch-or operations. Regarding swapping via XOR (the trick discussed earlier), it’s generally discouraged for production code: it’s harder to read, can misbehave if the two operands alias the same storage, and modern compilers optimize a normal std::swap very well. Readability and explicit helpers are usually the best choice.

Recommended Answers

All 14 Replies

Its bitwise OR operator. Read

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.