what is |= operator ? how it works ? can anybody give simple example ?
is it a bitwise OR operator ?
for example var1 = var1 | something
when i should use it ?
what is |= operator ? how it works ? can anybody give simple example ?
is it a bitwise OR operator ?
for example var1 = var1 | something
when i should use it ?
blue_gene
yes, it should be equivalent to:
var1 = var1 | var2;
so you would use it just as you would use say
var1 += var2;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It's useful when you're dealing with bit-flags. For example, when using Windows API:
style = WS_VISIBLE | WS_OVERLAPPED | WS_MINIMIZEBOX
But if you want to wrap your window-creation code in a function, for example (I probably wouldn't do this, but whatever):
Or, you could pass a parameter for 'additional flags', and do flags |= (the parameter). Stuff like that. As a note, the & operator is useful too, if you want to find out if a certain flag is set: if(flags & WS_VISIBLE) //then WS_VISIBLE bit is setCode:void createMyWindow(int something, int somethingElse, bool visible, bool minimizeBox) { DWORD flags = WS_OVERLAPPED; if(visible) flags |= WS_VISIBLE; if(minimizeBox) flags |= WS_MINIMIZEBOX; (...) CreateWindow(....., flags, .....); }
Hope this helps
Last edited by Hunter2; 04-27-2004 at 10:37 AM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
hunter, i dont have any idea about window API. so your code is out of my bound . i was expecting a simple example.
in fact i was expecting a simple bit level example. i think it is only for setting the bits. is not it?
thanks for the response
blue_gene
Those are bits. Your original thoughts were correct. Here is another example:Code:const int BIT_ONE = 0x0001; // 00000001 const int BIT_TWO = 0x0002; // 00000010 const int BIT_THREE = 0x0004; // 00000100 const int BIT_FOUR = 0x0008; // 00001000 const int BIT_FIVE = 0x0010; // 00010000 const int BIT_SIX = 0x0020; // 00100000 const int BIT_SEVEN = 0x0040; // 01000000 const int BIT_EIGHT = 0x0080; // 10000000 int main() { int value = BIT_ONE; // value is 00000001 in binary. value |= BIT_FOUR; // value is 00001001 in binary. value |= BIT_SEVEN; // value is 01001001 in binary. value |= BIT_FOUR; // value is 01001001 (still) in binary (bit four was already set). }
There is a really good introduction to bitwise operations in the Programming FAQ.
Most C++ programmers don't use binary or bitwise operations very often. This stuff is mostly used by hardware guys like me who probe-around with an oscilloscope all day, and by programmers who write firmware for embedded systems or programmers who write drivers.
For example, data bit zero might be connected to an error-LED (thru an 'address decoder' chip.), bit 1 might be connected to a transmit-LED. To turn-on a single LED without changing the state of all other LEDs, you have to use bitwise operations. Other bits can be used as single-switch inputs, or there might be a read-write register where each bit represents the status/state of something... Maybe bit 2 of the register represents "copy protection enabled". This is similar to Hunter2's example, except in the hardware world, the flags exists in a special physical register at a fixed-physical address.
As you probably know, all data is stored in binary insde the computer. C++ tries to insulate you from the binary, by automatically/transparently converting all input/output to/from decimal for you. In fact, you have to take some extra programming steps if you want to input or display the actual binary.
If you want to learn this stuff, it really helps if you are comfortable with binary and hexadecimal. Hex input/output is easy in C++ (easier than binary), and it is easy (for us humans) to convert between hex and binary... With a little practice you can do it in your head!!! (Converting between decimal and binary is not so easy... for humans).
Last edited by DougDbug; 04-27-2004 at 02:11 PM.
YES. As in jlou's example, operator |= will set particualar bits.i think it is only for setting the bits. is not it?
The most common bitwise "things to do", are:
Setting bits
Clearing bits
Testing bits... "If bit n is low...", or "if bit-n is high..."
Bit shifting and toggling are used less often.
>>Bit shifting and toggling are used less often.
Well, that's not 100% true. I do a lot of right-shift or left-shift when I'm trying to optimize code by getting rid of pesky division/multiplication operations... Although they probably get optimized out eventually anyways by the compiler.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
The & is quite useful as well, made a program to compare a given serial to a correct serial with this.
OS: Windows XP Pro CE
IDE: VS .NET 2002
Preferred Language: C++.
>>>I do a lot of right-shift or left-shift when I'm trying to optimize code by getting rid of pesky division/multiplication operations
...but that is limited. is not it ? bcoz bit shifting means multiply by 2 ^n or divide by 2^n so, you can not get all numbers. for example you can not gt odd numbers by doing bit shifting. so you are limited basically.
blue_gene
Yes you are quite limited, but for simple mathematics it is positively faster by an extreme amount.
OS: Windows XP Pro CE
IDE: VS .NET 2002
Preferred Language: C++.
[COLOR=Yellow]very simple yaar !![/COLOR]"hello | = C" is equivalent to "hello = hello | C".
cheers,
karthik bala guru.
Please don't use yellow for your post. It makes it hard to read.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.