Bitwise Operators in C The Bitwise operators supported by C language are listed in the following table. Assume variable A 60 and variable B holds 13, then: Operator Description
Example
&
Binary AND Operator copies a bit to the result if it exists in both operands.
(A & B) will give 12 which is 0000 1100
|
Binary OR Operator copies a bit if it exists in either operand.
(A | B) will give 61 which is 0011 1101
^
Binary XOR Operator copies the bit if it is set in one operand but not both.
(A ^ B) will give 49 which is 0011 0001
~
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
(~A ) will give -61 which is 1100 0011 in 2's complement form due a signed binary number.
<<
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
A << 2 will give 240 which is 1111 0000
>>
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
A >> 2 will give 15 which is 0000 1111
Example
Sign up to vote on this title
Useful
Not useful
Try the following example to understand all the bitwise operators available in C programming languag
Home
Saved
Top Charts
Books
Audiobooks
Magazines
News
Documents
Sheet Music
Upload
Sign In
Join
Search
Home
Saved
0
4 views
Upload
Sign In
RELATED TITLES
0
Bitwize Operator in C Uploaded by Ruchi Modi
Top Charts
Books
Audiobooks
Bitwize Operator in C
Save
Embed
Share
Print
Verilog Tutorial 1
1
Download
Magazines
News
Documents
Sheet Music
Join
of 6
c++
Arrays of Arrays Csharp
Search document
c = a ^ b; /* 49 = 0011 0001 */ printf("Line 3 - Value of c is %d\n", c ); c = ~a; /*-61 = 1100 0011 */ printf("Line 4 - Value of c is %d\n", c ); c = a << 2; /* 240 = 1111 0000 */ printf("Line 5 - Value of c is %d\n", c ); c = a >> 2; /* 15 = 0000 1111 */ printf("Line 6 - Value of c is %d\n", c ); }
When you compile and execute the above program it produces the following result: Line Line Line Line Line Line
1 2 3 4 5 6
-
Value Value Value Value Value Value
of of of of of of
c c c c c c
is is is is is is
12 61 49 -61 240 15
You're Reading a Preview
Detail Explanation
Unlock full access with a free trial.
Bitwise Operators : AND,OR,XOR
Download With Free Trial
AND,OR,XOR are three main Bitwise Operators in C Programming Language.AN Operator is used to mask particular Bits.Consider following table Input Bits 0
Output : Number1 AND Number2 : 8 Number OR Number2 : 14 Number XOR Number2 : 6
You're Reading a Preview Unlock full access with a free trial.
Download With Free Trial
Sign up to vote on this title
Useful
Not useful
Home
Saved
Top Charts
Books
Audiobooks
Magazines
News
Documents
Sheet Music
Upload
Sign In
Join
Search
Home
Saved
0
4 views
Upload
Sign In
RELATED TITLES
0
Bitwize Operator in C Uploaded by Ruchi Modi
Top Charts
Books
Audiobooks
Bitwize Operator in C
Save
Embed
Share
Print
Download
Magazines
News
Documents
Sheet Music
Join
Verilog Tutorial 1
1
of 6
c++
Arrays of Arrays Csharp
Search document
Bitwise Right Shift Operator in C
1. It is denoted by >> 2. Bit Pattern of the data can be shifted by specified number of Position Right 3. When Data is Shifted Right , leading zero’s are filled with zero. 4. Right shift Operator is Binary Operator [Bi - two] 5. Binary means , Operator that require two arguments
Quick Overview of Right Shift Operator Original Number A
0000 0000 0011 1100
Right Shift by 2
0000 0000 0000 1111
Leading 2 Blanks
Replaced by 0 ,Shown in RED
You're Reading a Preview Unlock full access with a free trial.
Direction of Movement of Data
Right ========>>>>>>
Download With Free Trial
Syntax : [variable]>>[number of places] Sign up to vote on this title
useful Useful Not Live Example : Bitwise Operator [Right Shift Operator]
#include
Home
Saved
Top Charts
Books
Audiobooks
Magazines
News
Documents
Sheet Music
Upload
Sign In
Join
Search
Home
Saved
0
4 views
Sign In
Upload
RELATED TITLES
0
Bitwize Operator in C Uploaded by Ruchi Modi
Top Charts
Books
Audiobooks
Bitwize Operator in C
Magazines
News
Documents
Sheet Music
Verilog Tutorial 1
Save
Embed
Share
Print
1
Download
Join
of 6
c++
Arrays of Arrays Csharp
Search document
Output : Number is Shifted By 1 Bit : 30 Number is Shifted By 2 Bits : 15 Number is Shifted By 3 Bits : 7
Bitwise Left Shift Operator in C
1. It is denoted by << 2. Bit Pattern of the data can be shifted by specified number of Position Left 3. When Data is Shifted Left , trailing zero’s are filled with zero. 4. Left shift Operator is Binary Operator [Bi - two] 5. Binary means , Operator that require two arguments
Quick Overview of Left Shift Operator You're Reading a Preview
Original Number A
0000 0000 0011 1100
Unlock full access with a free trial.
Left Shift Download With Free Trial 0000 0000 1111 0000
Trailing Zero’s
Replaced by 0 (Shown in RED)
Sign up to vote on this title
Direction of Movement of Data
Useful
Not useful
<<<<<=======Left
Home
Saved
Top Charts
Books
Audiobooks
Magazines
News
Documents
Sheet Music
Upload
Sign In
Join
Search
Home
Saved
0
4 views
Upload
Sign In
Join
RELATED TITLES
0
Bitwize Operator in C Uploaded by Ruchi Modi
Top Charts
Books
Audiobooks
Bitwize Operator in C
Save
Embed
Share
Print
1
Download
Magazines
News
Documents
Sheet Music
Verilog Tutorial 1
of 6
c++
Arrays of Arrays Csharp
Search document
{ int a
= 60;
printf("\nNumber is Shifted By 1 Bit : %d",a << 1); printf("\nNumber is Shifted By 2 Bits : %d",a << 2); printf("\nNumber is Shifted By 3 Bits : %d",a << 3); return(0);
}
Output : Number is Shifted By 1 Bit : 120 Number is Shifted By 2 Bits : 240 Number is Shifted By 3 Bits : 480
You're Reading a Preview Unlock full access with a free trial.