TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Understanding Bit Operator In C#
Lawal Abdulateef
Feb 14, 2017
4.8
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
In this blog, you will learn about bit operator in C#.
bitOperationcodesnipet.rar
& (AND) Operator
Binary AND Operator copies a bit to the result, if it exists in both the operands also returns true, if and only if both the operands are true. Unary variant returns the address of its operand. Binary variant is the bitwise AND of two operands.
A
B
& (AND) Operator
0
0
0
0
0
0
1
0
0
1
0
0
1
1
1
1
1
1
0
0
0
0
1
0
| OR Operator
Binary OR Operator copies a bit, if it exists in either operand i.e. true, if one or both operand is
true, false; if both the operands are false .
A
B
| (OR) Operator
0
0
0
0
0
0
1
0
1
1
0
1
1
1
1
1
1
1
0
0
0
0
1
1
^
XOR
Returns true, if and only if, one of the operands is true.
A
B
^(XOR ) Operator
0
0
0
0
0
0
1
0
1
1
0
1
1
1
0
1
1
0
0
0
0
0
1
1
~ Complement operator
The ~ operator performs a bitwise complement operation on its operand, which has the effect of reversing each bit. Bitwise complement operators are given below.
A
~
0
1
0
1
1
0
1
0
1
0
1
0
0
1
0
1
<<
Binary Left Shift Operator
The number of bits specified by the right operand moves the left operands value left. The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an integer or a type that has a predefined implicit numeric conversion to int. Thus, for example, 37 << 3 is shifting the number 37 to the left by 3 places. Of course, we are working with the binary representation of 37. The binary form of 37 is 00100101. After sliding by 3 to the left, we are left with 00101. However, we need to add three 0's to complete the bit 00101000.
>>
Binary
Right Shift Operator
The number of bits specified by the right operand moves the left operands value right. The right-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an integer or a type that has a predefined implicit numeric conversion to the integer. For example, 37 >> 3 is shifting the number 37 to the right by 3 places. Of course, we are working with the binary representation of 37 The binary form of 37 is 00100101. After sliding by 3 to the left, we are left with 00100. However, we need to add three 0's to complete the bit 00000100. Hope, the tutorial was helpful. Happy coding.
Bit Operator in C#
C#
Next Recommended Reading
Null Conditional Operator In C#