 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Jul 08, 2007 10:38 am Post subject: [TUT] Bitwise Operators in C# |
|
|
Ok, well here is my tutorial on how to perform bitwise operators in C#. If you don't know what a bitwise operator is, I suggest you read up on them--else you won't have the slighest clue about what's going on.
Also, I will assume you know about most of the C# commands already.
~~
So, the first step of performing a bitwise operation is turning your number(s) into binary.
--
FROM DECIMAL TO BINARY
--
Ok, create a new application (I'm using Microsoft Visual C# 2005 Express Edition, and I suggest you use it as well), and place a button, a text box, and a label on your form. Double click on the button to go to its code.
Code: |
Int64 BinaryHolder; //1
char[] BinaryArray;
string BinaryResult = "";
int _variable = int.Parse(textBox1.Text); //2
while (_variable > 0)
{
BinaryHolder = _variable % 2; //3
BinaryResult += BinaryHolder; //4
_variable = _variable / 2; //5
}
BinaryArray = BinaryResult.ToCharArray();
Array.Reverse(BinaryArray); //6
BinaryResult = new string(BinaryArray); //7
label1.Text = BinaryResult.ToString();
|
1: Here we're just labeling some needed variables...
2: You should know about the PARSE command, but if you don't then here's a description. What this does is basically takes a string variable and converts it to a type of number (such as double, int, long, etc).
3: The % sign means remainder. What this line does is divides the variable, _variable, by 2, and takes the remainder, then stores it in BinaryHolder.
4: You may not understand this now, but I will go over it soon.
5: Once again, you may not understand this, but I will go over it.
6: Well, our code has stored the binary result in reverse order, so we want to take it and flip it in the correct order.
7: So we make BinaryArray (which has been flipped into its correct order), and turn it into a string, then place it in our variable.
~~
Now I will explain the actual conversion.
So let's take a number, say 25. Look at the following table.
Code: |
25/2 = 12 r 1
12/2 = 6 r 0
6/2 = 3 r 0
3/2 = 1 r 1
1/2 = 0 r 1
|
Now, take the remainders that I lined up so carefully:
10011
But wait, that's not 25 in binary, now is it? Wait, let's reverse it.
11001
And there we go, 25 in binary. You understand the conversion now, correct?
--
FROM BINARY TO DECIMAL
--
Ok, so we've converted from decimal to binary succesfully, so now let's convert back. For ease's sake, we'll use the same form--just comment out the previous code for the moment.
Code: |
double n = double.Parse(textBox1.text);
int cnt = n.ToString().Length; //1
string p = n.ToString();
double a = 0;
while (cnt > 0)
{
foreach (char y in p.ToCharArray())
{
int c = int.Parse(y.ToString()); //2
if (c == 1)
{
a = a + (Math.Pow(2, cnt-1)); //3
}
else
{
}
cnt = cnt - 1; //4
}
}
label1.text = a.ToString();
|
1: This is a cool little function that allows you to have an integer which holds the length of a certain string.
2: Because Microsoft is an ass hole, not using this line would result in the numbers requiring to be ASCII (such as 48 = 0, 49 = 1, etc.), however this way we can use regular numbers, and not be confused. =D
3: Also another cool function, the Math.Pow, used for bringing a number to a certain power. I'll explain the 2 and cnt-1 later.
4: I will also explain.
So, let's do some binary to decimal.
How about the binary number:
0101
Well, let's look at the number 125 in decimal. This is equivelant to
Code: |
(1 * 10^2) + (2 * 10^1) + (5 * 10^0)
100 + 20 + 5
|
Now, as we know, you cannot have a "10" in a column in decimal. This works the same way with the number 2 and binary. So let's take our number, 0101, and try it.
Code: |
(0 * 2^3) + (1 * 2^2) + (0 * 2^1) + (1 * 2^0)
0 + 4 + 0 + 1
|
And we can deduce from this that 0101 is 5 in decimal.
--
AND
--
Ok, well, we could--should we want to--just do var1 & var2, and be done with it, but that's not what this tutorial is about. This tutorial is about hard core programming.
So, let's add another textbox, and let it keep its name. Let's review. The AND bitwise operator returns a 1 if both numbers are 1, otherwise it will return a 0. Or, we could think of it as... it will return a 1 if the sum of the two digits that it's messing with equal 2, and it will return a 0 if they equal otherwise.
Uncomment out your ToBinary thing on the button, and go make that its own class. ( public static string ToBinary(Double n) )
Code: |
double t = double.Parse(textBox1.Text);
double v = double.Parse(textBox1.Text);
string bin1 = ToBinary(t);
string bin2 = ToBinary(v);
double bin1a = double.Parse(bin1);
double bin2a = double.Parse(bin2);
double l = bin1a + bin2a;
string i = l.ToString();
double a = 0;
foreach (char o in i.ToCharArray())
{
int c = int.Parse(o.ToString());
if (c == 2)
{
if (a == 0) //1
{
a = 1;
}
else if (a != 0) //2
{
a = (a * 10) + 1;
}
}
else if (c != 2)
{
if (a == 0) //3
{
}
else if (a != 0) //4
{
a = (a * 10);
}
}
}
string b = ToDecimal((double)a); //5
label1.Text = double.Parse(b);
|
1: Well, think about it this way: The computer is still doing things in decimal, and therefore we only have the ability (or at least, I only have the ability, you may be able to do otherwise), to make them THINK that they're in binary. We could do *10 + 1, when a == 0, but it just seems a bit more professional this way.
2: If we just do a +1, like we did with the first one, we could end up with something like 12240, which is NOT binary.
3: If a == 0, and we're gonna have to return a 0, why do anything is there's already a 0?
4: That's as simple as it gets right there--just multiply it by 10, and you have another 0 on the end of it.
5: So now we perform the "ToDecimal" function on the double version of our final result, and boom. =D
--
OR
--
Now, as with the AND function, we could just do var1 | var2, however that's not the goal of this tutorial. Let's review, shall we? The OR function produces a 0 if both digits are 0, otherwise it will produce a 1. Hmm, that sounds simple right? So we return a 1 if our variable doesn't equal 0.
So comment out your AND code (or create a new button for the OR, it really doesn't matter), and put this in.
Code: |
double t = double.Parse(textBox1.Text);
double v = double.Parse(textBox1.Text);
string bin1 = ToBinary(t);
string bin2 = ToBinary(v);
double bin1a = double.Parse(bin1);
double bin2a = double.Parse(bin2);
double l = bin1a + bin2a;
string i = l.ToString();
double a = 0;
foreach (char o in i.ToCharArray())
{
int c = int.Parse(o.ToString());
if (c != 0)
{
if (a == 0)
{
a = 1;
}
else if (a != 0)
{
a = (a * 10) + 1;
}
}
else if (c == 0)
{
if (a == 0)
{
}
else if (a != 0)
{
a = (a * 10);
}
}
}
string b = ToDecimal((double)a);
label1.Text = double.Parse(b);
|
Now you may realize--"Hey wait a minute, we just used that code!". But, however many similarities you notice, there are a few differences that seperate the AND function from the OR function. This helps you realize that in reality, the two functions are really, very alike. You should understand this code perfectly fine, since we just went over it.
--
XOR
--
Ok, now the XOR function returns a 1 if both numbers are the same, and a 0 if they aren't the same. Now, if we think about it, then you can only have 2 types of numbers--a 1 and a 0, so in order for them to be different, they would have to be a 1 and 0, therefore equaling 1. Here is our modified code:
Code: |
double t = double.Parse(textBox1.Text);
double v = double.Parse(textBox1.Text);
string bin1 = ToBinary(t);
string bin2 = ToBinary(v);
double bin1a = double.Parse(bin1);
double bin2a = double.Parse(bin2);
double l = bin1a + bin2a;
string i = l.ToString();
double a = 0;
foreach (char o in i.ToCharArray())
{
int c = int.Parse(o.ToString());
if (c == 1)
{
if (a == 0)
{
a = 1;
}
else if (a != 0)
{
a = (a * 10) + 1;
}
}
else if (c != 1)
{
if (a == 0)
{
}
else if (a != 0)
{
a = (a * 10);
}
}
}
string b = ToDecimal((double)a);
label1.Text = double.Parse(b);
|
--
NOT
--
Now, this would be complicated as hell if it had two operands, but it doesn't, so it's really quite simple.
Code: |
long w = long.Parse(Input.Text);
string m = ToBinary((double)w);
double n = double.Parse(m);
double o = 0;
foreach (char x in m.ToCharArray())
{
int i = int.Parse(x.ToString());
if (i == 1)
{
if (o == 0)
{
o += 0;
}
else if (o != 0)
{
o = (o * 10);
}
}
if (i == 0)
{
if (o == 0)
{
o += 1;
}
else if (o != 0)
{
o = (o * 10) + 1;
}
}
}
string y = ToDecimal(o);
Input.Text = y;
|
You should understand this code pretty fine.
--
END
--
Well, I really hope you learned something from this, as that's what the aim of this tutorial was (like all tutorials). I'm sorry for all you VBers, and C++'ers, and such, because I don't do those languages, and therefore I cannot write a tut on them.
|
|
Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Sun Jul 08, 2007 1:44 pm Post subject: |
|
|
Why so complicated? I'm sure C# has bitwise logical operators...
Here: http://msdn2.microsoft.com/en-us/library/6a71f45d(vs.71).aspx
The operators are the same as the other C family languages.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Jul 08, 2007 2:40 pm Post subject: |
|
|
Ok, well I explained in the post that it has the AND and OR operators, but it doesn't have XOR and NOT, and so I decided to just post on how to do all of them.
Or at least, I didn't THINK they had XOR and NOT... >>;
|
|
Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Sun Jul 08, 2007 3:23 pm Post subject: |
|
|
Oh but it does:
^ for xor
~ for not
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|