We help IT Professionals succeed at work.

difference between conditional and logical operators in c#

rnsr
rnsr asked
on
Medium Priority
698 Views
Last Modified: 2012-05-11
Need help with example to explain
difference between conditional and logical operators in c#[ &,&&,|,||)
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
To expand upon the concept of "bitwise," think of all numbers as a string of bits (because in truth, they are). If you have the number 4 and you store it in an int variable, you have 32 bits to represent the number 4. These bits are the binary representation of the number. This is what 4 looks like in binary:

    0000 0000 0000 0000 0000 0000 0000 0100

There are, of course, no spaces in between these values--I added them for visual effect.

Now each of these positions can be either of two values: 1 (on) or 0 (off). If we have some series of related concepts that can either be on or off, then we can exploit this binary feature of our int variable to pack more data into the variable than just a simple number. This is the concept of bit flags. Each bit serves to indicate whether or not some value is on or off.

In my above example, lets say that the right-most index represents the "Compiled" flag, and the 2nd right-most represents the "IgnoreCase" flag. If both were on (and assuming nothing else was), then our int would look like this:

    0000 0000 0000 0000 0000 0000 0000 0011

As mentioned previously, we would use a bitwise OR to set these values (as demonstrated above). If we wanted to know whether or not these values had been set, then we would use a bitwise AND.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I must be sleepy  : )

My last example above, "Bitwise AND" should have been "Bitwise OR" and the example code was incorrect as well. It is corrected below.

Bitwise OR
bool iHaveMoney = true;
bool iHaveAFriendWithMoney = true;

if (iHaveMoney || iHaveAFriendWithMoney)
{
    // Either are true, so I can buy that new CD
}
else
{
    // Both were false, so I going to have to read a book instead.
}

Open in new window

Author

Commented:
Bitwise represent ON and Off concept. But programatically where can i use it in project.
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
Bitwise is used where the bits in a variable mean something like uppercase/lowercase or 'flags' for processes.  You can use "char & 0x5f" to make a character uppercase (at least in ASCII).  On this page http://msdn.microsoft.com/en-us/library/ms684863%28v=vs.85%29.aspx is an example of "Process Creation Flags" for Windows.  The constants names are usually used instead of the hex values.  To set the flags, you would normally use 'or' like "flagset | CREATE_BREAKAWAY_FROM_JOB" to include '0x01000000' in the value of 'flagset'.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
You could use bitwise AND to check to see if an int is odd or even. if ((num & 1) == 1)  will only be true if the last bit is a 1 which would make it an odd number.

Author

Commented:
i need some time to see it as i am busy for the past one week.
will respond soon.
Thanks,

Author

Commented:
Nice expalined with example
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.