
<<, >>
using System;
namespace ShiftOperator
{
class MainApp
{
static void Main(string[] args)
{
int a = 1;
Console.WriteLine("a: {0}", a); // 0000 0001 → 1
Console.WriteLine("a << 1: {0}", a << 1); // 0000 0010 → 2
Console.WriteLine("a << 2: {0}", a << 2); // 0000 0100 → 4
Console.WriteLine("a << 3: {0}", a << 3); // 0000 1000 → 8
int b = 32;
Console.WriteLine("\nb: {0}", b); // 0010 0000 → 32
Console.WriteLine("b >> 1: {0:}", b >> 1); // 0001 0000 → 16
Console.WriteLine("b >> 2: {0}", b >> 2); // 0000 1000 → 8
Console.WriteLine("b >> 3: {0}", b >> 3); // 0000 0100 → 4
int c = -64;
Console.WriteLine("\nc: {0}", c); // 1100 0000 → -64
Console.WriteLine("c >> 1: {0:}", c >> 1); // 1110 0000 → -32
Console.WriteLine("c >> 2: {0}", c >> 2); // 1111 0000 → -16
Console.WriteLine("c >> 3: {0}", c >> 3); // 1111 1000 → -8
}
}
}
[실행 결과]
a: 1
a << 1: 2
a << 2: 4
a << 3: 8
b: 32
b >> 1: 16
b >> 2: 8
b >> 3: 4
c: -64
c >> 1: -32
c >> 2: -16
c >> 3: -8
& (AND), | (OR), ^ (XOR), ~ (NOT)
using System;
namespace BitwiseOperator
{
class MainApp
{
static void Main(string[] args)
{
int a = 9;
int b = 10;
Console.WriteLine($"{a} & {b}: {a & b}"); // 1001 & 1010 → 1000
Console.WriteLine($"{a} | {b}: {a | b}"); // 1001 | 1010 → 1011
Console.WriteLine($"{a} ^ {b}: {a ^ b}"); // 1001 ^ 1010 → 0011
Console.WriteLine($"~{a}: {~a}"); // ~0000 1001 → 1111 0110
Console.WriteLine($"~{b}: {~b}"); // ~0000 1010 → 1111 0101
}
}
}
[실행 결과]
9 & 10: 8
9 | 10: 11
9 ^ 10: 3
~9: -10
~10: -11
▪ 사진 출처: Pixabay - Gerd Altmann