C Operator Bitwise: Operasi AND, OR, XOR, Complement dan Shift

Dalam tutorial ini Anda akan belajar tentang semua 6 operator bitwise dalam pemrograman C dengan contoh.

Dalam unit logika aritmatika (yang ada di dalam CPU), operasi matematika seperti: penjumlahan, pengurangan, perkalian dan pembagian dilakukan dalam tingkat bit. Untuk melakukan operasi bit-level dalam pemrograman C, operator bitwise digunakan.

Operator Arti dari operator
& Bitwise DAN
| Bitwise ATAU
^ Bitwise XOR
~ Pelengkap bitwise
<< Geser ke kiri
>> Geser ke kanan

Bitwise AND operator &

Output dari bitwise AND adalah 1 jika bit yang sesuai dari dua operan adalah 1. Jika salah satu bit dari sebuah operan adalah 0, hasil dari bit yang sesuai dievaluasi ke 0.

Misalkan operasi bitwise AND dari dua bilangan bulat 12 dan 25.

 12 = 00001100 (Dalam Biner) 25 = 00011001 (Dalam Biner) Operasi Bit 12 dan 25 00001100 & 00011001 ________ 00001000 = 8 (Dalam desimal)

Contoh # 1: Bitwise AND

 #include int main() ( int a = 12, b = 25; printf("Output = %d", a&b); return 0; ) 

Keluaran

 Keluaran = 8

Bitwise OR operator |

Output dari bitwise OR adalah 1 jika setidaknya satu bit yang sesuai dari dua operan adalah 1. Dalam Pemrograman C, operator bitwise OR dilambangkan dengan |.

12 = 00001100 (Dalam Biner) 25 = 00011001 (Dalam Biner) Bitwise ATAU Operasi 12 dan 25 00001100 | 00011001 ________ 00011101 = 29 (Dalam desimal)

Contoh # 2: Bitwise ATAU

 #include int main() ( int a = 12, b = 25; printf("Output = %d", a|b); return 0; ) 

Keluaran

 Keluaran = 29

Operator Bitwise XOR (OR eksklusif) ^

Hasil dari operator XOR bitwise adalah 1 jika bit yang sesuai dari dua operan berlawanan. Ini dilambangkan dengan ^.

 12 = 00001100 (Dalam Biner) 25 = 00011001 (Dalam Biner) Operasi Bitwise XOR 12 dan 25 00001100 00011001 ________ 00010101 = 21 (Dalam desimal)

Contoh # 3: Bitwise XOR

 #include int main() ( int a = 12, b = 25; printf("Output = %d", a^b); return 0; )

Keluaran

 Keluaran = 21

Operator pelengkap bitwise ~

Operator pujian bitwise adalah operator unary (bekerja hanya pada satu operan). Ini berubah 1 menjadi 0 dan 0 menjadi 1. Ini dilambangkan dengan ~.

 35 = 00100011 (Dalam Biner) Operasi pelengkap bitwise dari 35 ~ 00100011 ________ 11011100 = 220 (Dalam desimal) 

Putar di operator pelengkap bitwise dalam Pemrograman C.

Pelengkap bitwise dari 35 (~ 35) adalah -36 dan bukan 220, tapi mengapa?

Untuk bilangan bulat n apa pun, komplemen bitwise dari n adalah -(n+1). Untuk memahami ini, Anda harus memiliki pengetahuan tentang pelengkap 2.

Pelengkap 2

Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the complement of that number plus 1. For example:

 Decimal Binary 2's complement 0 00000000 -(11111111+1) = -00000000 = -0(decimal) 1 00000001 -(11111110+1) = -11111111 = -256(decimal) 12 00001100 -(11110011+1) = -11110100 = -244(decimal) 220 11011100 -(00100011+1) = -00100100 = -36(decimal) Note: Overflow is ignored while computing 2's complement. 

The bitwise complement of 35 is 220 (in decimal). The 2's complement of 220 is -36. Hence, the output is -36 instead of 220.

Bitwise complement of any number N is -(N+1). Here's how:

 bitwise complement of N = ~N (represented in 2's complement form) 2'complement of ~N= -(~(~N)+1) = -(N+1) 

Example #4: Bitwise complement

 #include int main() ( printf("Output = %d",~35); printf("Output = %d",~-12); return 0; ) 

Output

 Output = -36 Output = 11

Shift Operators in C programming

There are two shift operators in C programming:

  • Right shift operator
  • Left shift operator.

Right Shift Operator

Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by>>.

 212 = 11010100 (In binary) 212>>2 = 00110101 (In binary) (Right shift by two bits) 212>>7 = 00000001 (In binary) 212>>8 = 00000000 212>>0 = 11010100 (No Shift) 

Left Shift Operator

Left shift operator shifts all bits towards left by a certain number of specified bits. The bit positions that have been vacated by the left shift operator are filled with 0. The symbol of the left shift operator is <<.

 212 = 11010100 (In binary) 212<<1 = 110101000 (In binary) (Left shift by one bit) 212<<0 = 11010100 (Shift by 0) 212<<4 = 110101000000 (In binary) =3392(In decimal)

Example #5: Shift Operators

 #include int main() ( int num=212, i; for (i=0; i>i); printf(""); for (i=0; i<=2; ++i) printf("Left shift by %d: %d", i, num< 
 Right Shift by 0: 212 Right Shift by 1: 106 Right Shift by 2: 53 Left Shift by 0: 212 Left Shift by 1: 424 Left Shift by 2: 848 

Artikel yang menarik...