Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ออเปอเรเตอร์ Left Shift และ Right Shift (>> และ <<) ใน C#

Bitwise Left Shift Operator (<<)

ค่าของตัวถูกดำเนินการด้านซ้ายจะถูกเลื่อนไปทางซ้ายตามจำนวนบิตที่ระบุโดยตัวดำเนินการด้านขวา

Bitwise Right Shift Operator (>>)

ค่าของตัวถูกดำเนินการด้านซ้ายจะถูกเลื่อนไปทางขวาตามจำนวนบิตที่ระบุโดยตัวดำเนินการด้านขวา

ตัวอย่าง

using System;

namespace Demo {
    class Program {
        static void Main(string[] args) {
            int a = 60;  // 60 = 0011 1100
            int b = 13;  // 13 = 0000 1101
            int c = 0;
            
            c = a << 2;  // 240 = 1111 0000
            Console.WriteLine("Value of c is {0}", c);
            
            c = a >> 2;  // 15 = 0000 1111
            Console.WriteLine("Value of c is {0}", c);
            
            Console.ReadLine();
        }
    }
}

ผลลัพธ์

Value of c is 240
Value of c is 15