Bitwise Operator

1 10 2011
Bitwise Operator = Digunakan untuk operasi bit per bit pada nilai integer. Terdiri dari operator NOT, AND, OR, XOR, Shl, Shr. Type : int atau char
Bitwise operator, dari namanya sudah jelas diketahui pasti berhubungan dgn bit. Biasanya digunakan utk memanipulasi data bertipe bit dari tipe data integer atau byte. Bitwise ini sering dipakai dalam kasus enkripsi,kompresi,format file,jaringan(protokol),processing audio/video,dll.
Macam-macam Bitwise:
1.Bitwise And (&)
2.Bitwise OR / inclusive OR (|)
3.Bitwise XOR / Exclusive OR (^)
4.Bitwise Complement (~)
Bitwise AND
Kalau kedua operand adalah 1,maka hasilnya 1. Selain itu hasilnya 0.
contoh:
13 & 11 = 9 <— mengapa hasilnya 9?
mari kita uraikan:
13 = 00000000 00000000 00000000 00001101
11 = 00000000 00000000 00000000 00001011
————————————————————– &
00000000 00000000 00000000 00001001 <- hasilnya 9
Bitwise OR
Kalau kedua operand adalah 0,maka hasilnya 0. Selain itu hasilnya 1.
contoh:
13 | 11 = 15 <— mengapa hasilnya 11?
mari kita uraikan:
13 = 00000000 00000000 00000000 00001101
11 = 00000000 00000000 00000000 00001011
————————————————————– |
00000000 00000000 00000000 00001111 <- hasilnya 15
Bitwise XOR
Kalau kedua operand adalah 1 atau 0,maka hasilnya adalah 0. Selain itu hasilnya 1.
contoh:
13 ^ 11 = 6 <— mengapa hasilnya 6?
13 = 00000000 00000000 00000000 00001101
11 = 00000000 00000000 00000000 00001011
————————————————————– ^
00000000 00000000 00000000 00000110 <- hasilnya 6
Bitwise Complement
Nilai operand akan menjadi bernilai negatif dan dikurangi 1.
contoh:
1.~0
2.~5
3.~-0
4.~-5
~0 = -1
~5 = -6
~-0 = -1
~-5 = 4
bit 1
bit 2
AND
OR
XOR
0
0
0
0
0
1
0
0
1
1
0
1
0
1
1
1
1
1
1
0

There are following Bitwise operators supported by C language

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100
| Binary OR Operator copies a bit if it exists in eather operand. (A | B) will give 61 which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001
~ Binary Ones Complement Operator is unary and has the efect of ‘flipping’ bits. (~A ) will give -60 which is 1100 0011
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111

Sumber : http://roulysoufinasiahaan.posterous.com/operator-bitwise

http://www.tutorialspoint.com/ansi_c/c_operator_types.htm


Actions

Information

One response

16 10 2013
karina

tengkyu gan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: