SA-MP Forums Archive
[Tutorial] An in-depth look at binary and binary operators. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] An in-depth look at binary and binary operators. (/showthread.php?tid=177523)

Pages: 1 2 3


An in-depth look at binary and binary operators. - Kyosaur - 18.09.2010

Navigation

What is binary?

Binary is a numeral system that uses two unique symbols to represent numbers. While the more common decimal system uses ten numerals (base 10), binary uses only 0 and 1. This may sound useless in every day life, but binary is essential when it comes to computers. Computers at their lowest level perform all of their calculations by manipulating the flow of electricity to indicate on and off states. This is exactly what binary is, just a ton of switches flipped on and off. This is a sort of alien concept to most people, so lets take a look at the decimal and binary system next to each other.


Decimal (base 10)
pawn Code:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
Binary (Base 2)
pawn Code:
0 //0
1 //1
10 //2
11 //3
100 //4
101 //5
110 //6
111 //7
1000 //8
1001 //9
1010 //10
1011 //11
1100 //12
1101 //13
Looking at both systems beside one another, you'll notice they behave exactly the same. Once you reach the last available number you have to move on to another place. These places in binary are referred to as bits (binary digits) and are simply powers of two; just as places in the decimal system are powers of 10. To prove this, lets take a look at the number 13 in standard notation.


NOTE: '^' is power in these next few examples, not bitwise exclusive (which we'll cover later.


Decimal (base 10)
pawn Code:
13

//which equals

1 * (10^1) + 3 * (10^0)

//which equals

10+3

//which equals

13
Binary (base 2)
pawn Code:
1101

//which equals

1 * (2^3) + 1 * (2^2) + 0 * (2^1) + 1 * (2^0)

//which equals

8+4+0+1

//which equals

13
We can see from the preceding example that if a bit is set to 0, we can ignore it and move on; after all, anything multiplied by 0 is going to be 0. The previous example was a little over complicated and was just me trying to being absolutely clear. When you're converting from binary, all you really have to worry about is adding up the powers of all the bits that are turned on.

Here are 12 powers of 2 just off the top of my head:

pawn Code:
4096,2048,1024,512,256,128,64,32,16,8,4,2,1
If you know nothing about working with powers, this probably makes no sense to you at all. A power is a number multiplied by itself x amount of times. With this information in mind, the preceding list of powers probably makes more sense; well with the exception of 1. You may be curious why 2 raised to the power of 0 gives a result of 1, all i can say to this is that it just does.

pawn Code:
2^1 = 2, 2^3 = 4, 2^4 = 8
We can see that when we move to the right, our previous value is multiplied by 2; so its safe to assume that when we move to the left our new value is just the previous number divided by 2. With this in mind you can see how we can end up with 2 to the zeroth power equaling 1. If this isn't satisfying enough, im sure you can find more proof on ******. All that being said, lets take a look at one final example, and lets make it somewhat complicated!

pawn Code:
111011001011111000 //242424

//Remember, ignore the bits that arent turned on.

1 * (2^17) = 131072

1 * (2^16) = 65536

1 * (2^15) = 32768

1 * (2^13) = 8192

1 * (2^12) = 4096

1 * (2^9) = 512

1 * (2^7) = 128

1 * (2^6) = 64

1 * (2^5) = 32

1 * (2^4) = 16

1 * (2^3) = 8


131072+65536+32768+8192+4096+512+128+64+32+16+8
=
242424
Remember when converting: The first power is 0 so dont make the mistake as seeing the 18th place as 2^18. There are indeed 18 powers, but that is including the power of 0, so 17 is actually our highest power.



A deeper look at bits

Most programming languages allow different data types which range in the amount of bits that can be used to store information; however pawn is a typeless 32 bit language. This means that pawn will always have 32 bits available for storing information. So what happens when you have to much information? The answer to that question lies with signed and unsigned integers.

Binary operators

Binary operators allow you to manipulate individual bits of a bit pattern. Lets take a look at a list of available bitwise operators.




Re: An in-depth look at binary and binary operators. - Kyosaur - 18.09.2010

Reserved.


EDIT: inb4 "0b" for binary numbers (i'll add it in, i just forgot it ).


Re: An in-depth look at binary and binary operators. - LarzI - 18.09.2010

Great tutorial!
Learned alot from it!

I still (as I'm only 16 years old) don't know in what situation I can take use of binary, but oh well.. I'll find out sooner or later.

Again, amazingly done!


Re: An in-depth look at binary and binary operators. - Hiddos - 18.09.2010

The thing that's so fun about binary is that you can have any possible numbers, even when you think it seems impossible lmao.

Learned a lot from this, and I also mentioned that a specific bit equals all previous bits turned on minus one, for example:

pawn Code:
1000000 //64
 111111 //63
This is great for me, even though I don't quite exactly know when I could use it haha.

I'm also wondering on one thing: Is it possible to use this system (And it's operators) in PAWN?


Re: An in-depth look at binary and binary operators. - Leeroy. - 18.09.2010

you could have also showed Hexadecimal to binary which is commonly used in pawno.

Nice tut


Re: An in-depth look at binary and binary operators. - Kyosaur - 18.09.2010

Quote:
Originally Posted by LarzI
View Post
Great tutorial!
Learned alot from it!

I still (as I'm only 16 years old) don't know in what situation I can take use of binary, but oh well.. I'll find out sooner or later.

Again, amazingly done!
Well, look at the key checking method everyone uses :P. That right there uses the bitwise AND operator.


You can use binary as a way to store multiple bool values (can stuff 31 bools in a single variable) as well. To do this you would use bitwise AND once again to check if the bit is set, and use the exclusive operator to toggle it on and off. Y_less has a pre-written system in YSI which allows you to store more than 31 values (using arrays of course), i haven't personally checked it out yet though.


Off topic: First one to figure out what this macro does wins a high five!

Code:
#define FUNC_NAME(%0) ((%0) & 0x80000000)?(~(%0) + 0b1):(%0)
Its not efficient by any means (there is a WAY easier way to do it, and it also pwns this one in speed), but its a nice little "test" thing i guess.


Edit:

My grammar has gone to hell, i obviously need to get some sleep lol.


@Hiddos - yes, everything in here can be used in pawn. Also, when using the operators the numbers do not have to be a physical binary number (which in pawn is preceded by "0b" like hex is with "0x" :P) i would hope everyone would already understand after reading my post lol. Things like:

Code:
Tmp = Var << 3;
Tmp = Var & 4;
Tmp = Var ^ 5;

//and 

Var <<= 3;
Var &= 4;
Var ^= 5;

//etc
work perfectly fine.


Re: An in-depth look at binary and binary operators. - LarzI - 18.09.2010

Quote:
Originally Posted by Hiddos
View Post
The thing that's so fun about binary is that you can have any possible numbers, even when you think it seems impossible lmao.

Learned a lot from this, and I also mentioned that a specific bit equals all previous bits turned on minus one, for example:

pawn Code:
1000000 //64
 111111 //63
This is great for me, even though I don't quite exactly know when I could use it haha.

I'm also wondering on one thing: Is it possible to use this system (And it's operators) in PAWN?
If it's possible to use binary in PAWN?
Well of course! That's why he made this tutorial.. x)

Look up the PAWN language guide! (Y)


Re: An in-depth look at binary and binary operators. - Mimic - 18.09.2010

Nicely done great tutorial


Re: An in-depth look at binary and binary operators. - Hiddos - 18.09.2010

Quote:
Originally Posted by Kyosaur
View Post
First one to figure out what this macro does wins a high five!

Code:
#define FUNC_NAME(%0) ((%0) & 0x80000000)?(~(%0) + 0b1):(%0)
Just an idea: It translates hexadecimal code to binary code D

And thanks LarzI, I'll try something out then hehe.


Re: An in-depth look at binary and binary operators. - Kyosaur - 18.09.2010

Quote:
Originally Posted by Hiddos
View Post
Just an idea: It translates hexadecimal code to binary code D

And thanks LarzI, I'll try something out then hehe.
ok, if you say so:

Code:
0b10000000000000000000000000000000
Using a physical binary number is a pain, as they can get pretty long lol.


Edit:

Sorry i misread this, i thought you asked me to convert the hex to binary. No, it doesnt convert hex into binary. Hexadecimal and binary are just different numeral systems, there is nothing to convert if the values are the same. The only thing different between hex and binary is how they represent our value.

Code:
0b1011  //binary representation for 11
0xB      //Hex representation for 11
Both the above equal 11, its just they are represented differently, which is noted by the pawn constant "0b" for binary and pawn constant "0x" for hex.


Re: An in-depth look at binary and binary operators. - DiddyBop - 18.09.2010

errr you lost me @ "Binary is a numeral system that uses two unique symbols to represent numbers"


Re: An in-depth look at binary and binary operators. - Simon - 19.09.2010

Quote:
Originally Posted by Kyosaur
View Post
Off topic: First one to figure out what this macro does wins a high five!

Code:
#define FUNC_NAME(%0) ((%0) & 0x80000000)?(~(%0) + 0b1):(%0)
Its not efficient by any means (there is a WAY easier way to do it, and it also pwns this one in speed), but its a nice little "test" thing i guess.
It converts a signed (signed for negative) 32-bit binary number into a two's complement binary number .


Re: An in-depth look at binary and binary operators. - Kyosaur - 19.09.2010

Quote:
Originally Posted by DiddyBop
View Post
errr you lost me @ "Binary is a numeral system that uses two unique symbols to represent numbers"
Basically "Numbers in binary only use 1's and 0's". Thought that was pretty clear lol.


Quote:
Originally Posted by Simon
View Post
It converts a signed (signed for negative) 32-bit binary number into a two's complement binary number .
*Kyoshiro High-fives Simon

Awkward wording, but yes . It gets the absolute value of a number.



The more obvious (and more efficient) implementation would be:

Code:
#define abs(%0) (((%0) < 0)?(-(%0)):((%0)))
if anyone was curious lol.


Re: An in-depth look at binary and binary operators. - Backwardsman97 - 20.09.2010



Nice post lol. That's what I looked like when I read it all.


Re: An in-depth look at binary and binary operators. - Hiddos - 23.09.2010

Took some time to make this awesome system save in my mind, I'm still wondering about this: What is the correct usage for binary in pawn, when working with variables? Does it work a bit like:

pawn Code:
new var = 68;
if(var & 32) print("Var has the 6th bit switched on!");
Or is it more like how it's exactly stated:

pawn Code:
new var = 68;
if(var & 100000) print("Var has the 6th bit switched on!");
Any advice would be great, it'd be fun to experiment to get a single variable into a bool of 31 arrays (Yes, I know somebody will be a jerk about that )


Re: An in-depth look at binary and binary operators. - Calgon - 23.09.2010

Great tutorial.


Re: An in-depth look at binary and binary operators. - Kyosaur - 23.09.2010

Quote:
Originally Posted by Hiddos
View Post
Took some time to make this awesome system save in my mind, I'm still wondering about this: What is the correct usage for binary in pawn, when working with variables? Does it work a bit like:

pawn Code:
new var = 68;
if(var & 32) print("Var has the 6th bit switched on!");
Or is it more like how it's exactly stated:

pawn Code:
new var = 68;
if(var & 100000) print("Var has the 6th bit switched on!");
Any advice would be great, it'd be fun to experiment to get a single variable into a bool of 31 arrays (Yes, I know somebody will be a jerk about that )
Both are correct as they are exactly the same, its just a matter of preference (Note: physical binary numbers need "0b" preceding them though). I'd go with the first example or even use hex, as binary numbers can get pretty big.

Quote:
Originally Posted by Calgon
View Post
Great tutorial.
Thanks .


Re: An in-depth look at binary and binary operators. - Chaprnks - 24.09.2010

Very useful tutorial. I never knew what most of those operators actually did until now.


Re: An in-depth look at binary and binary operators. - Slice - 25.09.2010

Example usage of bitwise XOR and bitwise AND:
This will give you a variable (s_iChangedKeys) with only the keys that were just pressed down.
You can, for example, do:
pawn Code:
if ( s_iChangedKeys & KEY_FIRE )
{
    // ...
}

pawn Code:
new
    g_iPreviousKeys[ MAX_PLAYERS ] // This variable will hold the previous key state for each player
;

public OnPlayerUpdate( playerid )
{
    // I use static in OnPlayerUpdate because the varaible won't have to get re-initialized every call; therefore, less work for the CPU.
   
    static
        s_iKeys,
        s_iUnused, // blah blah
        s_iChangedKeys
    ;
   
    GetPlayerKeys( playerid, s_iKeys, s_iUnused, s_iUnused );
   
    s_iChangedKeys = ( s_iKeys ^ g_iPreviousKeys[ playerid ] );
   
    // First, filter out the bits that were 1 in both variables; leaving only the bits that has changed.
   
    s_iChangedKeys = s_iChangedKeys & s_iKeys;
   
    // Second, keep only the bits that are 1 in both variables.
    // Now, s_iChangedKeys contain only bits that was off in g_iPreviousKeys and on in s_iKeys.
   
    // You can also save a line by doing: s_iChangedKeys = ( s_iKeys ^ g_iPreviousKeys[ playerid ] ) & s_iKeys;
   
    if ( s_iChangedKeys & KEY_FIRE )
    {
        SendClientMessage( playerid, -1, "you just pressed fire" );
    }
   
    g_iPreviousKeys[ playerid ] = s_iKeys;

    return 1;
}



Re: An in-depth look at binary and binary operators. - MrDeath537 - 26.09.2010

Very very nice tutorial, so, when should I use binary?

Thanks you.