Friday 9 March 2018

Bitwise - Rahasia Kalkulasi Super Cepat


Tadinya saya sempat tidak paham kenapa anak TOKI yang menjadi pemateri ToT ICT bisa melakukan komputasi yang seharusnya dilakukan dengan kurun waktu +- 25 detik menjadi hanya 1 detik. Tapi sampai saat tulisan ini  saya tulis pun masih belum paham kok. :-)
Intinya, penggunaan BitWise ini dapat merubah nilai dari desimal menjadi Biner (0,1), sehingga membuat komputer mudah mengerti dan melakukan komputasi / perhitungan pun menjadi lebih mudah.

Misal:

Comparing Bits: The Bitwise and Operator
Bitwise and is a binary operator that uses the following syntax
Operand1 and Operand2
This operator considers two values and compares the bit of each with the corresponding bit of the other value. If both corresponding bits are 1, the comparison produces 1. Otherwise, that is, if either bit is 0, the comparison produces 0. This comparison is resumed as follows:
Bit1Bit2Bit1 and Bit2
000
100
010
111
Imagine you have two byte values represented as 187 and 242. Based on Appendix C, the binary value of decimal 187 is 1011 1011 (and its hexadecimal value is 0xBB). The binary value of decimal 242 is 1111 0010 (and its hexadecimal value is 0xF2). Let’s compare these two values bit by bit, using the bitwise AND operator:
 Binary Decimal
N110111011187
N211110010242
N1 and N210110010178

Most of the times, you will want the compiler to perform this operation and use the result in your program. This means that you can get the result of this operation and possibly display it on the console. The above operation can be performed by the following program:
program Project1;

{$APPTYPE CONSOLE}

const N1 = 187;
const N2 = 242;

begin
 Writeln(N1, ' and ', N2, ' = ', (N1 and N2));

 Write('Press any key to continue...');
 Readln;
end.
This would produce:
187 and 242 = 178
Press Enter key to exit...


Source: http://www.functionx.com/objectpascal/Lesson09.htm

No comments: