GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Bitwise Operators
Selected Reading
C# - Bitwise OperatorsC# bitwise operators allow operations at the binary level. These operators are primarily used for performance optimization, low-level programming, encryption, networking, and system programming. What Are Bitwise Operators in C#?C# bitwise operators manipulate individual bits of numbers. These operators are commonly used for efficient computations, flag operations, and hardware interactions. List of C# Bitwise OperatorsThe Bitwise operators supported by C# are listed in the following table. Assume variable A holds 60 and variable B holds 13, then −
Understanding Bitwise Operations with ExamplesExample 1: Bitwise AND (&)Bitwise AND sets each bit to 1 only if both operands have 1 in that position. using System;
class Program
{
static void Main()
{
int a = 5, b = 3; // Binary: a = 0101, b = 0011
int result = a & b; // 0101 & 0011 = 0001 (1)
Console.WriteLine("Bitwise AND: " + result);
}
}
When the above code is compiled and executed, it produces the following result − Bitwise AND: 1 Example 2: Bitwise OR (|)Bitwise OR sets each bit to 1 if either operand has 1. using System;
class Program
{
static void Main()
{
int a = 5, b = 3; // Binary: a = 0101, b = 0011
int result = a | b; // 0101 | 0011 = 0111 (7)
Console.WriteLine("Bitwise OR: " + result);
}
}
When the above code is compiled and executed, it produces the following result − Bitwise OR: 7 Example 3: Bitwise XOR (^)Bitwise XOR sets each bit to 1 if the corresponding bits are different. using System;
class Program
{
static void Main()
{
int a = 5, b = 3; // Binary: a = 0101, b = 0011
int result = a ^ b; // 0101 ^ 0011 = 0110 (6)
Console.WriteLine("Bitwise XOR: " + result);
}
}
When the above code is compiled and executed, it produces the following result − Bitwise XOR: 6 Example 4: Bitwise NOT (~)Bitwise NOT inverts each bit (turns 1 into 0 and vice versa). using System;
class Program
{
static void Main()
{
int a = 5; // Binary: 00000101
int result = ~a; // NOT 00000101 = 11111010 (Twos Complement)
Console.WriteLine("Bitwise NOT: " + result);
}
}
When the above code is compiled and executed, it produces the following result − Bitwise NOT: -6 Example 5: Left Shift (<<)Left shift (<<) moves bits left, multiplying the number by 2^n. using System;
class Program
{
static void Main()
{
int a = 5; // Binary: 00000101
int result = a
Advertisements
| |||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |