Raised This Month: $ Target: $400
 0% 

[HELP] Switching two variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-27-2008 , 12:04   Re: [HELP] Switching two variables
Reply With Quote #1

SnoW, yes it can:
Code:
stock replaceint(&a, &b) {     new temp = a;     a = b;     b = temp; }

daniel, won't that only work because those are 2 numbers that are able to do that?
What about 3 and 7?
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 12-27-2008 , 12:49   Re: [HELP] Switching two variables
Reply With Quote #2

Quote:
Originally Posted by Exolent[jNr] View Post
SnoW, yes it can: Code:
stock replaceint(&a, &b) { new temp = a; a = b; b = temp; }


daniel, won't that only work because those are 2 numbers that are able to do that?
What about 3 and 7?
It works for everything
Code:
new a = 3, b = 7 //a = 011b, b = 111b)

a ^= b // 011b ^ 111b = 100b
b ^= a // 111b ^ 100b = 011b
a ^= b // 100b ^ 011b = 111b

// a == 7, b == 3 //a = 111b, b = 011b)
__________________

Community / No support through PM
danielkza is offline
AoD90
Senior Member
Join Date: Jul 2008
Location: Ugljevik/RS/BIH
Old 12-27-2008 , 14:51   Re: [HELP] Switching two variables
Reply With Quote #3

This all works. Thanks.

Is there way to switch two words too?

Last edited by AoD90; 12-28-2008 at 03:17.
AoD90 is offline
Send a message via MSN to AoD90
AoD90
Senior Member
Join Date: Jul 2008
Location: Ugljevik/RS/BIH
Old 12-28-2008 , 07:16   Re: [HELP] Switching two variables
Reply With Quote #4

Quote:
Originally Posted by AoD90 View Post
This all works. Thanks.

Is there a way to switch two words too?
AoD90 is offline
Send a message via MSN to AoD90
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 12-28-2008 , 07:29   Re: [HELP] Switching two variables
Reply With Quote #5

[OT]@danielkza: So if the 2 bits are different, the result will be the bit that is less close to 000?

i.e:
010 XOR 011 = 011? // 011 > 010? Or it doesn't work like that with bits?
100 XOR 011 = 100? // 100 > 011?[/OT]


@AoD90:

Try this:
PHP Code:
// lenz = number of characters to swap.
stock swap_wordswordA[], wordB[], lenz )
{
    new 
temp250 ]; // 250 will be the maximum number of character the wordA can contain. Edit this to your liking.
//    You must make sure that lenz is NOT bigger than temp's size.
    
    
formattemplenz 1"%s"wordA );
    
formatwordAlenz 1"%s"wordB );
    
formatwordBlenz 1"%s"temp );
    
    return 
1;

Example of usage:
PHP Code:
#include <amxmodx>

// lenz = number of characters to swap.
stock swap_wordswordA[], wordB[], lenz )
{
    new 
temp250 ]; // 250 will be the maximum number of character the wordA can contain. Edit this to your liking.
//    You must make sure that lenz is NOT bigger than temp's size.
    
    
formattemplenz 1"%s"wordA );
    
formatwordAlenz 1"%s"wordB );
    
formatwordBlenz 1"%s"temp );
    
    return 
1;
}

public 
plugin_init()
{
    
register_clcmd"say /swap""myFunction" );
}

public 
myFunctionid )
{
    new 
HelloWorld[] = "HelloWorld!";
    new 
WorldHello[] = "WorldHello!";
    
    
swap_wordsHelloWorldWorldHello11 );
    
client_printidprint_chat"HelloWorld: %s    WorldHello:    %s"HelloWorldWorldHello );

__________________
O o
/¯________________________
| IMMA FIRIN' MAH LAZOR!!!
\_¯¯¯

Last edited by Dores; 12-28-2008 at 08:05.
Dores is offline
AoD90
Senior Member
Join Date: Jul 2008
Location: Ugljevik/RS/BIH
Old 12-28-2008 , 07:42   Re: [HELP] Switching two variables
Reply With Quote #6

I got some error:

Error:Must be a constant expression; assumed zero on line 98
Error:Invalid array size (negative or zero) on line 100
Error:Undefined symbol "temp" on line 100
AoD90 is offline
Send a message via MSN to AoD90
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 12-28-2008 , 08:06   Re: [HELP] Switching two variables
Reply With Quote #7

Fixed last post.
Test and tell me the results.
__________________
O o
/¯________________________
| IMMA FIRIN' MAH LAZOR!!!
\_¯¯¯
Dores is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 12-28-2008 , 15:29   Re: [HELP] Switching two variables
Reply With Quote #8

Quote:
Originally Posted by Dores View Post
[OT]@danielkza: So if the 2 bits are different, the result will be the bit that is less close to 000?

i.e:
010 XOR 011 = 011? // 011 > 010? Or it doesn't work like that with bits?
100 XOR 011 = 100? // 100 > 011?[/OT]
If the two XORed bits are different, the result will be 1, if they're equal it will be 0.

E.g. 10110011 XOR 01001100 = 11111111

Nothing besides that, that's the only calculation done, nothing involving higher or lower numbers.
__________________

Community / No support through PM
danielkza is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 12-29-2008 , 09:21   Re: [HELP] Switching two variables
Reply With Quote #9

Ohhh ok I think I got it!
Thanks, +karma.

Sorry for off-topic.
__________________
O o
/¯________________________
| IMMA FIRIN' MAH LAZOR!!!
\_¯¯¯
Dores is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:10.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode