Bitwise Logical Operators in C

By: Grenfel Emailed: 1699 times Printed: 2200 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Three bitwise logical operators are used to manipulate individual bits in an integer data type, as shown in Table below. These operators have names similar to the TRUE/FALSE logical operators, but their operations differ.

The bitwise logical operators.

Operator Description
& AND
| Inclusive OR
^ Exclusive OR

These are all binary operators, setting bits in the result to 1 or 0 depending on the bits in the operands. They operate as follows:

  • Bitwise AND sets a bit in the result to 1 only if the corresponding bits in both operands are 1; otherwise, the bit is set to 0. The AND operator is used to turn off, or clear, one or more bits in a value.
  • Bitwise inclusive OR sets a bit in the result to 0 only if the corresponding bits in both operands are 0; otherwise, the bit is set to 1. The OR operator is used to turn on, or set, one or more bits in a value.
  • Bitwise exclusive OR sets a bit in the result to 1 if the corresponding bits in the operands are different (if one is 1 and the other is 0); otherwise, the bit is set to 0.

The following are examples of how these operators work:

 
Operation Example
AND 11110000
  & 01010101
  ----------------
  01010000
Inclusive OR 11110000
  | 01010101
  ----------------
  11110101
Exclusive OR 11110000
  ^ 01010101
  ----------------
  10100101

You just read that bitwise AND and bitwise inclusive OR can be used to clear or set, respectively, specified bits in an integer value. Here's what that means. Suppose you have a type char variable, and you want to ensure that the bits in positions 0 and 4 are cleared (that is, equal to 0) and that the other bits stay at their original values. If you AND the variable with a second value that has the binary value 11101110, you'll obtain the desired result. Here's how this works:

In each position where the second value has a 1, the result will have the same value, 0 or 1, as was present in that position in the original variable:

0 & 1 == 0
1 & 1 == 1

In each position where the second value has a 0, the result will have a 0 regardless of the value that was present in that position in the original variable:

0 & 0 == 0
1 & 0 == 0

Setting bits with OR works in a similar way. In each position where the second value has a 1, the result will have a 1, and in each position where the second value has a 0, the result will be unchanged:

0 | 1 == 1
1 | 1 == 1
0 | 0 == 0
1 | 0 == 1

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

hi i have doubt in strlen()........pls explain how it works..........with example of palindrome string program

View Tutorial          By: ranjith at 2010-04-18 21:50:17

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Grenfel
Verifying a Signed JAR File example
Bitwise and Bit Shift Operators example in Java
JavaBeans Basic Concepts
The Differences Between Simple and Classic Tags in JSP
Bitwise Logical Operators in C
DispatchAction in Struts
union example program in C
Comparison operators in JSP
Basics of Structures in C
register Variables in C
Struts and Tiles - Steps to use Struts and Tiles
The if else statement in C
Character Counting sample program in C
Installing JSF
cin.ignore() in C++

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
Sum of the elements of an array in C
Find square and square root for a given number in C
UNIX read and write system calls sample program in C
Open, Creat, Close, Unlink system calls sample program in C
perror() Function - example program in C
Relational and Logical Operators in C
Conditional Expressions in C
Using realloc() Function in C
assert() Function Example program in C
Formatting with printf in C
Printing a simple histogram in C
Variables and Arithmetic Expressions in C
Character Arrays in C
Precedence and Order of Evaluation in C
Most Emailed Articles (in C)
External Variables and Scope in C
Symbolic Constants using #define in C
Address Arithmetic and pointers in C
Tutorial on Complicated Declarations in C
lseek() sample program in C
Using malloc() Function in C
Using memset(), memcpy(), and memmove() in C
Bitwise Logical Operators in C
Infix to Prefix And Postfix in C
Passing pointer to a function in C
Getting Started with C
File Copying in C
Data Types and Sizes in C
The if else statement in C
else if statement in C