ShawnjohnSJ
2012-04-11 03:38:06
Yakman's bitwise tutorial was created back in 2007 and I realized it didn't show many practical uses, in this tutorial I will show a few of them. I assume whoever is reading this already has basic knowledge of integers and bytes.
Plain and simply, bits are the next step down from bytes. In every byte, there are 8 bits - just like there are 1024 bytes in a kilobyte. Unlike bytes which can hold a value up to 255, bits can only have the value '0' or '1', this is called binary. A bit can be either unset or set - when a bit is set, it will equal '1', when it is unset it will equal '0'. Bits can be manipulated for many things: as flags, packing multiple bytes into an integer, encryption, compression, swapping two integers without a temporary variable, and many other things. I will cover some of these. Also if you ever want to work close to hardware or emulate hardware with software, you will need some good knowledge of bits.
So lets get started with the bit operators. There are five different operators: OR, AND, XOR, SHL, and SHR. I'll be explaining everything in comments within the code. Starting each script will show you the operations in the console.
OR:
AND:
XOR:
SHL:
SHR:
Plain and simply, bits are the next step down from bytes. In every byte, there are 8 bits - just like there are 1024 bytes in a kilobyte. Unlike bytes which can hold a value up to 255, bits can only have the value '0' or '1', this is called binary. A bit can be either unset or set - when a bit is set, it will equal '1', when it is unset it will equal '0'. Bits can be manipulated for many things: as flags, packing multiple bytes into an integer, encryption, compression, swapping two integers without a temporary variable, and many other things. I will cover some of these. Also if you ever want to work close to hardware or emulate hardware with software, you will need some good knowledge of bits.
So lets get started with the bit operators. There are five different operators: OR, AND, XOR, SHL, and SHR. I'll be explaining everything in comments within the code. Starting each script will show you the operations in the console.
OR:
program BitwiseOR;
var
myInt, yourInt, resultInt : Integer;
// You don't need to know anything about this function. It simply converts a
// decimal number to binary format for use in this tutorial.
function IntToBin(value : Integer) : string;
begin
repeat
if((value mod 2) = 0) then
result := '0' + result
else
result := '1' + result;
value := value / 2;
until(value = 0);
end;
begin
ClearDebug;
// Lets say the integer myInt equals 113 and the integer yourInt equals 73.
// Hint: You can change these two numbers to anything you like to learn!
// (For the purpose of this tutorial, keep them at 113 and 73 until the end)
myInt := 113 // 1110001
yourInt := 73; // 1001001
Writeln('myInt = ' + IntToStr(myInt) + ' (' + IntToBin(myInt) + ')');
Writeln('yourInt = ' + IntToStr(yourInt) + ' (' + IntToBin(yourInt) + ')');
// In binary form, 113 would be equal to '1110001' and 73 would be
// equal to '1001001'. When using the OR operator on two integers, it compares
// the two integers and says: "Okay, if one of or both bits equal a '1', then
// the resulting bit will be a '1'. If neither bit equals '1', the resulting
// bit will be a '0'."
resultInt := myInt or yourInt;
Writeln('-------------------------');
// myInt = 1110001
// yourInt = 1001001 OR
// ------------------------------
// resultInt = 1111001 (121)
// As you can see, the first four bits of resultInt were set to '1'
// because one or both bits in myInt and yourInt were '1'. The next two
// bits were unset to '0' because neither myInt or yourInt were equal to '1'.
// The last bit was set because both myInt and yourInt equaled '1'.
// Here you can see the resultInt equals 121 (1111001)
Writeln('resultInt = ' + IntToStr(resultInt) + ' (' + IntToBin(resultInt) + ')');
end.
AND:
program BitwiseAND;
var
myInt, yourInt, resultInt : Integer;
// You don't need to know anything about this function. It simply converts a
// decimal number to binary format for use in this tutorial.
function IntToBin(value : Integer) : string;
begin
repeat
if((value mod 2) = 0) then
result := '0' + result
else
result := '1' + result;
value := value / 2;
until(value = 0);
end;
begin
ClearDebug;
// Lets say the integer myInt equals 113 and the integer yourInt equals 73.
// Yes, just like last time!
// Hint: You can change these two numbers to anything you like to learn!
// (For the purpose of this tutorial, keep them at 113 and 73 until the end)
myInt := 113 // 1110001
yourInt := 73; // 1001001
Writeln('myInt = ' + IntToStr(myInt) + ' (' + IntToBin(myInt) + ')');
Writeln('yourInt = ' + IntToStr(yourInt) + ' (' + IntToBin(yourInt) + ')');
// In binary form, 113 would be equal to '1110001' and 73 would be
// equal to '1001001'. When using the AND operator on two integers, it compares
// the two integers and says: "Okay, if both bits equal a '1', then
// the resulting bit will be a '1'. Otherwise, the resulting bit will be a '0'."
resultInt := myInt and yourInt;
Writeln('-------------------------');
// myInt = 1110001
// yourInt = 1001001 AND
// ------------------------------
// resultInt = 1000001 (65)
// As you can see, the first bit of resultInt was set to a '1'
// because both bits in myInt and yourInt were '1'. The next five
// bits were unset to '0' because neither myInt or yourInt had both bits set to '1'.
// The last bit was set because both myInt and yourInt equaled '1'.
// Here you can see the resultInt equals 65 (1000001)
Writeln('resultInt = ' + IntToStr(resultInt) + ' (' + IntToBin(resultInt) + ')');
end.
XOR:
program BitwiseXOR;
var
myInt, yourInt, resultInt : Integer;
// You don't need to know anything about this function. It simply converts a
// decimal number to binary format for use in this tutorial.
function IntToBin(value : Integer) : string;
begin
repeat
if((value mod 2) = 0) then
result := '0' + result
else
result := '1' + result;
value := value / 2;
until(value = 0);
end;
begin
ClearDebug;
// Lets say the integer myInt equals 113 and the integer yourInt equals 73.
// Yes, just like the last two times! (I like to stay consistent!)
// Hint: You can change these two numbers to anything you like to learn!
// (For the purpose of this tutorial, keep them at 113 and 73 until the end)
myInt := 113 // 1110001
yourInt := 73; // 1001001
Writeln('myInt = ' + IntToStr(myInt) + ' (' + IntToBin(myInt) + ')');
Writeln('yourInt = ' + IntToStr(yourInt) + ' (' + IntToBin(yourInt) + ')');
// In binary form, 113 would be equal to '1110001' and 73 would be
// equal to '1001001'. When using the XOR operator on two integers, it compares
// the two integers and says: "Okay, if one if the bits, but not both, equals a '1', then
// the resulting bit will be a one. But if both bits equal 1, or neither of them equal '1',
// the resulting bit is a '0'"
resultInt := myInt xor yourInt;
Writeln('-------------------------');
// myInt = 1110001
// yourInt = 1001001 AND
// ------------------------------
// resultInt = 0111000 (56) (You can simplify this as 111000 because
// if there is a 0 at the front, its meaningless
// and gets cut off.)
// As you can see, the first bit of resultInt was unset to a '0'
// because both bits in myInt and yourInt were '1'. The next three
// bits were set to '1' because for all three bits, either myInt or yourInt had
// a bit set to '1'. The next two bits were unset because neither myInt or
// yourInt had bits that equaled '1'. The last bit is unset to '0' because both bits
// equaled '1'.
// Here you can see the resultInt equals 56 (111000)
Writeln('resultInt = ' + IntToStr(resultInt) + ' (' + IntToBin(resultInt) + ')');
end.
SHL:
program BitwiseSHL;
var
myInt, resultInt : Integer;
// You don't need to know anything about this function. It simply converts a
// decimal number to binary format for use in this tutorial.
function IntToBin(value : Integer) : string;
begin
repeat
if((value mod 2) = 0) then
result := '0' + result
else
result := '1' + result;
value := value / 2;
until(value = 0);
end;
begin
ClearDebug;
// For this example, we will only have one integer: myInt will be set to 113 (1110001).
// Hint: You can change this number to anything you like to learn!
// (For the purpose of this tutorial, keep it at 113)
myInt := 113; // 1110001
Writeln('myInt = ' + IntToStr(myInt) + ' (' + IntToBin(myInt) + ')');
// In binary form, 113 would be equal to '1110001'. The SHL operator is a
// little different from the previous three operators. SHL is an abbreviation
// for "shift left". It does exactly what it sounds like - it shifts all of
// the bits to the left by a specified number of times. For now, we will
// shift myInt to the left one time.
resultInt := myInt shl 1;
Writeln('-------------------------');
// myInt = 1110001 SHL
// ------------------------------
// resultInt = 11100010 (226)
// So looking here, when we shifted myInt to the left one time, we got the
// value 11100010. But wait! Where did this extra '0' come from at the end?
// Well, when you shift to the left, you need a place holder for the positions
// left behind. So if you shift left twice, you would end up with two extra
// '0's and so on. It should be noted that when you shift to the left,
// its exactly like multiplying by two. So "226 shl 1" would equal 452 and so forth.
// Here you can see the resultInt equals 226 (11100010)
Writeln('resultInt = ' + IntToStr(resultInt) + ' (' + IntToBin(resultInt) + ')');
end.
SHR:
program BitwiseSHR;
var
myInt, resultInt : Integer;
// You don't need to know anything about this function. It simply converts a
// decimal number to binary format for use in this tutorial.
function IntToBin(value : Integer) : string;
begin
repeat
if((value mod 2) = 0) then
result := '0' + result
else
result := '1' + result;
value := value / 2;
until(value = 0);
end;
begin
ClearDebug;
// For this example, we will only have one integer: myInt will be set to 113 (1110001).
// Hint: You can change this number to anything you like to learn!
// (For the purpose of this tutorial, keep it at 113)
myInt := 113; // 1110001
Writeln('myInt = ' + IntToStr(myInt) + ' (' + IntToBin(myInt) + ')');
// In binary form, 113 would be equal to '1110001'. The SHR operator is similiar
// to the previous SHL operator. SHR is an abbreviation for "shift right".
// It does exactly what it sounds like - it shifts all of the bits to the right
// by a specified number of times. For now, we will shift myInt to the right one time.
resultInt := myInt shr 1;
Writeln('-------------------------');
// myInt = 1110001 SHL
// ------------------------------
// resultInt = 111000 (56)
// So looking here, when we shifted myInt to the right one time, we got the
// value 111000. As you can see, the '1' on the very end of myInt was cut off.
// When you shift to the right, the bit to the very right will always be cut off,
// that's just the way it is. The bit disappears forever and when you shift left, it will
// not magically reappear. It should also be noted that when you shift to the right,
// its exactly like dividing by two. So "113 shr 1" would equal 56 and so forth.
// Here you can see the resultInt equals 56 (111000)
Writeln('resultInt = ' + IntToStr(resultInt) + ' (' + IntToBin(resultInt) + ')');
end.