Jump to content
  • 0
Zirius

what does comparison "<<" do?

Question

I am wondering what does "<<" do in euphy's WOE controller?

 

if ($WOE_CONTROL[.@i+3]&(1<<.@j)) mes "  ~ "+getcastlename(.Castles$[.@j])+" ^777777("+.Castles$[.@j]+")^000000";

 

Thanks!

Share this post


Link to post
Share on other sites

11 answers to this question

Recommended Posts

  • 0

1. correct

 

by the way this script isn't mine

my original script from eathena use OnTue20000: something-like labels

asking members to put in the labels for start time and end time

( see my King of Emperium Hill script )

its Euphy that he improvise to make this script configurable in-game

 

 

 

2.

in SQL, to remove a value is actually 'NULL', and '0' is a value

in athena, '0' means both

 

also means, if SQL doesn't list a value, it means 0 with athena script language

 

your values are

 

1st session has woe on Sunday (0), starts from 12am (0) ~ 1am (1)

with the castle ID 32 = 2^5

"payg_cas01" <- this 1 castle is on

 

2nd session has woe on Sunday (0), starts from 1am (1) ~ 2am (2)

with the castle ID 4 = 2^2

"prtg_cas03" <-- this 1 castle is on

 

3rd session has woe on Thursday (4), starts from 10pm (22) ~ 11pm (23)

with the castle ID 32 = 2^5

"payg_cas01" <-- this 1 castle is on

 

4th session has woe on Friday (5), starts from 10pm (22) ~ 11pm (23)

with the castle ID 4 = 2^2

"prtg_cas03" <-- this 1 castle is on

 

 

 

3.

prontera,156,184,5	script	sdfhsdkfjs	100,{	.@nb = query_sql( "select `index`, `value` from mapreg where varname = '$WOE_CONTROL'", .@index, .@value );	for ( .@i = 0; .@i < .@nb; .@i++ )		.@woe[ .@index[.@i] ] = .@value[.@i];	dispbottom "There are currently "+ .@nb / 4 +" sessions configured";	for ( .@i = 0; .@i < .@nb; .@i += 4 ) {		dispbottom " --- Session No."+( .@i/4 +1 )+" --- ";		dispbottom "War on "+ .days$[ .@woe[.@i] ] +". Starts from "+ .@woe[.@i+1] +":00 until "+ .@woe[.@i+2] +":00";		.@c = 0;		deletearray .@castle$;		for ( .@j = 0; .@j < 30; .@j++ )			if ( .@woe[.@i+3] & 1 << .@j )				.@castle$[.@c++] = .castles$[.@j];		dispbottom "at these castles : "+ implode( .@castle$, "," );	}	end;OnInit:	setarray .days$[0],"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday";	setarray .castles$[0],		"prtg_cas01","prtg_cas02","prtg_cas03","prtg_cas04","prtg_cas05",		"payg_cas01","payg_cas02","payg_cas03","payg_cas04","payg_cas05",		"gefg_cas01","gefg_cas02","gefg_cas03","gefg_cas04","gefg_cas05",		"aldeg_cas01","aldeg_cas02","aldeg_cas03","aldeg_cas04","aldeg_cas05",		"arug_cas01","arug_cas02","arug_cas03","arug_cas04","arug_cas05",		"schg_cas01","schg_cas02","schg_cas03","schg_cas04","schg_cas05";	end;}

Share this post


Link to post
Share on other sites
  • 0
Logical bitwise operators work only on numbers, and they are the following: << - Left shift. >> - Right shift.	Left shift moves the binary 1(s) of a number n positions to the left,     which is the same as multiplying by 2, n times.	In the other hand, Right shift moves the binary 1(s) of a number n     positions to the right, which is the same as dividing by 2, n times.		Example:		b = 2;		a =  b << 3;		mes a;		a = a >> 2;		mes a;	The first mes command would display 16, which is the same as:     2 x (2 x 2 x 2) = 16.	The second mes command would display 4, which is the same as:     16 / 2 = 8; 8 / 2 = 4.

script_commands.txt says it all.

P.S: Its not a comparision sign, its shift signs(binary)

Share this post


Link to post
Share on other sites
  • 0

This is going to make me cry. Have no knowledge with binary.

 

Bro, say, i = 0, and j=0

 

what is this trying to call?

$WOE_CONTROL[.@i+3]&(1<<.@j)

 

also, what does & do in that function? and what actually is it trying to do?

Share this post


Link to post
Share on other sites
  • 0

This is going to make me cry. Have no knowledge with binary.

 

Bro, say, i = 0, and j=0

 

what is this trying to call?

$WOE_CONTROL[.@i+3]&(1<<.@j)

 

also, what does & do in that function? and what actually is it trying to do?

Suppose if

$WOE_CONTROL[0],1,3,4,7;

i=0 that means .@i+3 = 0+3 = 3, it will call $WOE_CONTROL[3](which have value 7 as mentioned on above line)

So if .@j = 2

(1<<2) = 4, putting all in the statement , it will look like "7&4" Which will be true, & means that the first value contains second value or not(don't know how to explain if you are not familiar with binary)

 

& Defination on Script_Command:

The bitwise operator AND (&) is used to test two values against each     other, and results in setting bits which are active in both arguments.     This can be used for a few things, but in Hercules this operator is     usually used to create bit-masks in scripts.

Bitwise Opeartions Defination on Wikipedia: http://en.wikipedia.org/wiki/Bitwise_operation

Share this post


Link to post
Share on other sites
  • 0

 

This is going to make me cry. Have no knowledge with binary.

 

Bro, say, i = 0, and j=0

 

what is this trying to call?

$WOE_CONTROL[.@i+3]&(1<<.@j)

 

also, what does & do in that function? and what actually is it trying to do?

Suppose if

$WOE_CONTROL[0],1,3,4,7;

i=0 that means .@i+3 = 0+3 = 3, it will call $WOE_CONTROL[3](which have value 7 as mentioned on above line)

So if .@j = 2

(1<<2) = 4, putting all in the statement , it will look like "7&4" Which will be true, & means that the first value contains second value or not(don't know how to explain if you are not familiar with binary)

 

& Defination on Script_Command:

The bitwise operator AND (&) is used to test two values against each     other, and results in setting bits which are active in both arguments.     This can be used for a few things, but in Hercules this operator is     usually used to create bit-masks in scripts.

Bitwise Opeartions Defination on Wikipedia: http://en.wikipedia.org/wiki/Bitwise_operation

 

Thank you very much for patience bro.

 

if (7&4)

will return true because 7 contains 4?

did I understand it correctly?

Share this post


Link to post
Share on other sites
  • 0

 

Thank you very much for patience bro.

 

if (7&4)

will return true because 7 contains 4?

did I understand it correctly?

7&4 will return 4

7&1 will return 0

 

Want to try those values yourself? Open your Calculator(provided with windows) and use Programmer Mode

for typing & , press "AND" button, "<<" = "LSHFT", ">>" = "RSHFT"

 

Edit: o_O Annie Viewing topic, probably would answer better than me.

Share this post


Link to post
Share on other sites
  • 0

LOL. I guess I should start watching binary introduction videos.

 

What I am just trying to do is understand how Euphy stored the castlename's data. I successfully made it to "echo" the data and store them all in single array via external PHP file. but if I create more than 1 session, I do not understand it anymore. LOL.

 

Actually, I just want to convert this into PHP language:

 

		mes "[Schedule]";		if (.Size) {			freeloop(1);			for(set .@i,0; .@i<.Size; set .@i,.@i+4) {				mes "> ^FF0000"+.Days$[$WOE_CONTROL[.@i]]+" ("+Add_Zero($WOE_CONTROL[.@i+1])+"-"+Add_Zero($WOE_CONTROL[.@i+2])+")^000000";				for(set .@j,0; .@j<30; set .@j,.@j+1)					if ($WOE_CONTROL[.@i+3]&(1<<.@j)) mes "  ~ "+getcastlename(.Castles$[.@j])+" ^777777("+.Castles$[.@j]+")^000000";				if (.@i+4 < .Size) mes " ";			}			freeloop(0);		} else			mes "No times are configured.";		next;

this is as far as I got:

	$Castles=array(		"prtg_cas01","prtg_cas02","prtg_cas03","prtg_cas04","prtg_cas05",		"payg_cas01","payg_cas02","payg_cas03","payg_cas04","payg_cas05",		"gefg_cas01","gefg_cas02","gefg_cas03","gefg_cas04","gefg_cas05",		"aldeg_cas01","aldeg_cas02","aldeg_cas03","aldeg_cas04","aldeg_cas05",		"arug_cas01","arug_cas02","arug_cas03","arug_cas04","arug_cas05",		"schg_cas01","schg_cas02","schg_cas03","schg_cas04","schg_cas05");	$Days = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");	$result = mysql_query('SELECT * FROM mapreg WHERE varname="$WOE_CONTROL"');	$WOE_CONTROL = array();	while($row = mysql_fetch_array($result)) {	  $WOE_CONTROL[$row['index']] = $row['value'];	}	$x=0;	while($x<count($WOE_CONTROL)) {		echo '<br />';		echo $Days[$WOE_CONTROL[$x]];		echo '<br />';		echo $WOE_CONTROL[$x+1] .":00 - ". $WOE_CONTROL[$x+2] .":00";			$j=0;			while($j<30) {				if ($WOE_CONTROL[$x+3]&(1<<$j)) {					echo "  ~ ".$Castles$[$j];				}				if ($x+4 < count($WOE_CONTROL)) {echo " "};				$j=j+1;			}		$x=$x+4;	}

Share this post


Link to post
Share on other sites
  • 0

Edit: o_O Annie Viewing topic, probably would answer better than me.

LOL ... you did well though

I thought can let you handle ... oh well

 

anyways,

play around with this little script that I wrote during eathena period ... sh*t eathena forum down

so I make a new one

prontera,156,184,5	script	ksdkfjhdkshfj	100,{	.@difficulty = 4; // change this from 1 to 30 ( athena script engine allows up to 2^32 -1 )	mes "Guess the total value of ^0000FF"+( .@value = rand( 1, 1 << .@difficulty ) )+"^000000 ?";	next;	while (1) {		.@menu$ = "";		for ( .@i = 0; .@i < .@difficulty; .@i++ )			.@menu$ = .@menu$ +( ( .@answer & 1 << .@i )? "^00FF00" : "^FF0000" )+"1 << "+ .@i +":";		.@s = select( .@menu$ ) -1;		.@answer = .@answer ^ 1 << .@s;		if ( .@answer == .@value ) break;	}	mes "Correct ! The answer is ";	for ( .@i = 0; .@i < .@difficulty; .@i++ ) {		if ( .@answer & 1 << .@i ) {			.@ans$[.@c] = 1 << .@i;			.@c++;		}	}	mes implode( .@ans$, "+" );	close;}
1 << 0 = 1 ( also means 20 -> 2 power of 0 -> you press the scientific calculator as 2^0 )

1 << 1 = 2 ( also means 21 -> 2 power of 1 -> you press the scientific calculator as 2^1 )

1 << 2 = 4 ( also means 22 -> 2 power of 2 -> you press the scientific calculator as 2^2 )

... and so on

 

so, if the script randoms a number of 14

the correct answer is 2+4+8

which means

1<<1 + 1<<2 + 1<<3

also means

21 + 22 + 23

 

also means

14 & 0 == false

14 & 1 == true

14 & 2 == true

14 & 4 == true

 


 

in euphy script, which actually ... copy mine ... I wish I can show you the topic in eathena

"prtg_cas01" uses 1<<0 ... "prtg_cas02" use 1<<1 and so on ( see setarray .Castles$[0] )

so if

$WOE_CONTROL[0] = 0

$WOE_CONTROL[1] = 1

$WOE_CONTROL[2] = 2

$WOE_CONTROL[3] = 3

it means, the 1st session has woe on Sunday(0), starts from 1am(1) ~ 2am(2)

with the castle ID 1+2 enable (3)

1+2 = prtg_cas01 + prtg_cas02 <-- these 2 castle on war at the time

 

if you configure another session

$WOE_CONTROL[4] = 3

$WOE_CONTROL[5] = 20

$WOE_CONTROL[6] = 22

$WOE_CONTROL[7] = 96

means 2nd session has woe on Wednesday (3), starts from 8pm (20) ~ 10pm(22)

with the castle ID 96 = 32+64 = 25 + 26

1<<5 + 1<<6 = "payg_cas01" and "payg_cas02" <-- these 2 castles are enabled

Edited by AnnieRuru

Share this post


Link to post
Share on other sites
  • 0

WOW. I do not actually know what to say. I never knew binaries are that cool, it seems you can store soooo many data in just n-digit number.
 
1) Thank you very much for making me understand those shift operators, I understand them faster and clearer than those I read on google. for now "&" is the only thing I am having confusion.
 
BTW, about the WOE controller script, if I understood it correctly mam,
you are storing the data by multiple of four, one[0] for the day, one[1] for the start time, one[2] for the end time, and one[3] for the castle
 
2) But on $WOE_CONTROL, there would come a time that the "index" will not be continuous, just like now, my current $WOE_CONTROL is now:
 

Array(    [2] => 1    [3] => 32    [5] => 1    [6] => 2    [7] => 4    [8] => 4    [9] => 22    [10] => 23    [11] => 32    [12] => 5    [13] => 22    [14] => 23    [15] => 4)

may I know mam what algorithm did you used to create those index? I coded a loop that starts at zero and do the function recursively be 4. The problem is, as shown in my current WOE_CONTROL, index 0 is missing, and some other index numbers, so my code fails.
 
3) In your example mam you have this:
 

$WOE_CONTROL[4] = 3$WOE_CONTROL[5] = 20$WOE_CONTROL[6] = 22$WOE_CONTROL[7] = 96

index [7] stores the castle name, is there a way I can decode that to show me all the integers used to build that bit? (actually, I remember having the same need for solution).

 

P.S. Is there restriction at quoting Annie Ruru's forum posts? This had been my 4th time having problem quoting her replies.

Edited by Zirius

Share this post


Link to post
Share on other sites
  • 0

I can right now answer you to the first one question (the '&' operator) so that you'll have it nice and clear.

 

& is the bitwise AND operator, and it's done between two numbers. This means the operator does a bit-by-bit AND comparing both numbers. AND operator means: something is true (1) if A AND B are both true. Else something is false (0).

 

Let's make an example to it. What's 5 & 11?

 

First, let's place the numbers in binary form:

 

05 is 0101 in binary form;

11 is 1011 in binary form.

 

Now let's compare them bit by bit. A bit result is 1 if that bit from the first operand and from the second operand are both true, so:

 

0101 <--- 5

1011 <--- 11

-----

0001 <--- 1

 

Now you see 5 & 11 = 1.

 

Try for example what (4 & 11), (6 & 11) and (7 & 11) operations yield.

 

I'll leave the rest of the questions to miss Annie ;P

Share this post


Link to post
Share on other sites
  • 0

@AnnieRuru

 

Thank you very much that's all I need.

 

It is working great now, I am calling it via external PHP now, i plan on integrating this as a FluxCP module/addon.

 

post-6720-0-62769500-1409470180_thumb.png

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.