About bitwise or | operator

Started by proximity, Mon 27/03/2006 15:03:53

Previous topic - Next topic

proximity

 i was trying to make SortNumbers module (with 6 numbers) last week and i did it actually. The problem is, module got 4500 lines.

Ã,  Description: i have 6 numbers(int) and i wanted to make them sort correctly first on top and last on the bottom. So i did something like this:

function SortNumbers(int a, int b, int c, int d, int e, int f) {
if ( (a>b)&(a>c)&(a>d)&(a>e)&(a>f) ) { // a is first
Ã,  Ã, // Code here;
}......

But like this, i had to define all winners, seconds, thirds so on. it lies to 4500 lines. Then i come up with a new idea and tried this:

Ã,  Ã, if ( a>(b&c&d&e&f) ) { // a is first

Ã,  Ã, }
Ã,  Ã, if ( b >(a&c&d&e&f) ) { // b is first
Ã,  Ã, ...................
Ã,  Ã, ...................
Ã,  Ã, 
Ã,  Ã, if ( (b|c|d|e|f) > (b|c|d|e|f ) > (a) > (b|c|d|e|f ) > (b|c|d|e|f ) > (b|c|d|e|f ) ) { // a is third
Ã, 
Ã,  Ã,  ..................
Ã,  Ã,  ..................

Ã,  Ã, That was easier and approximately 300 lines. But i had a problem, AGS engine, thought every expressions as true and ran every command. So, the result was: f is first, f is second, f is third, f is fourth, f is fifth and f is sixth.

Afterwards, i tried five numbers:

function SortNumbers(int a, int b, int c, int d, int e) {

}

Suprisingly, it worked fine. i just thought it is because of bitwise or | operator.

Can anyone tell me what does | operator actually do ? More than 3 | in one paranthesis can not work ? How do Windows OSÃ,  sort everything that easy ?
Ã,  Ã,  Ã, 

Proximity Entertainment

SSH

#1
You'd probably be best using one of the classic sorting algorithms, anyway (google for "sorting algorithms") bubble sort, selection sort, insertion sort. For 6 numbers, probably bubble sort is easiest:

Code: ags

function SortNumbers(int a, int b, int c, int d, int e, int f) {
int n[6];
n[0]=a; n[1]=b; n[2]=c; n[3]=d; n[4]=e; n[5]=f;
int i;
int swap=1;
while (swap) {
Ã,  swap=0;
Ã,  i=1;
Ã,  while (i<6) {
Ã,  Ã,  if (n[i] < n[i-1]) {
Ã,  Ã,  Ã,  int temp=n[i];
Ã,  Ã,  Ã,  n[i]=n[i-1];
Ã,  Ã,  Ã,  n[i-1]=temp;
Ã,  Ã,  Ã,  swap=1;
Ã,  Ã,  }
Ã,  Ã,  i++;
Ã,  }
}
Ã,  // Do something with result!
}



The logic of your bitwise operations is completely broken, I don't know why you think you can do it like that. The bitwsie operator is to do with binary logic, which if you don't know what it is then forget about it...
12

Radiant

To find out what bitwise or does... it's a mathematical operation on two parameters. Thus a|b|c = (a|b) | c, just like a+b+c = (a+b) + c.

Then what is a|b ?  To find out, write out both numbers in binary, below one another. Underneath, write the result. Each binary digit (bit) of the result is ONE if either or both of the digits above it are ONE; it is ZERO if none of those digits are ONE.

Code: ags

0 | 0 = 0 
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

39 | 11 =
000100111 |
000001011 =
000101111 = 47


But then usually you don't need such binary logic.

proximity

#3
Thanks SSH but i don't get something. can i use this to define what will happen when a is second or c is fifth. i mean, for example, i want to declare when a is second , Display ( "a is second ");  Can i declare this ?

Edit:

Thanks Radiant, now i get what | do ? it's something different than i thought. it's a math

  However, & and && are same thing to me. are they ? i use & on my operations and everytime it works like && operator
Proximity Entertainment

SSH

#4
Code: ags

function SortNumbers(int a, int b, int c, int d, int e, int f) {
Ã,  int n[6];
Ã,  char nn[6];
Ã,  n[0]=a; n[1]=b; n[2]=c; n[3]=d; n[4]=e; n[5]=f;
 int i=0;
Ã,  while (i<6) {
Ã,  Ã,  nn[i]='a'+i;
Ã,  Ã,  i++;
Ã,  }

Ã,  int swap=1;
Ã,  while (swap) {
Ã, Ã,  Ã, swap=0;
Ã, Ã,  Ã, i=1;
Ã, Ã,  Ã, while (i<6) {
Ã,  Ã, Ã,  Ã, if (n[i] < n[i-1]) {
Ã,  Ã,  Ã, Ã,  Ã, int temp=n[i];
Ã,  Ã,  Ã,  Ã,  char temp2=nn[i];
Ã,  Ã,  Ã, Ã,  Ã, n[i]=n[i-1];
Ã,  Ã,  Ã, Ã,  Ã, n[i-1]=temp;
Ã,  Ã,  Ã,  Ã,  nn[i]=nn[i-1];
Ã,  Ã,  Ã,  Ã,  nn[i-1]=temp2;
Ã,  Ã,  Ã, Ã,  Ã, swap=1;
Ã,  Ã, Ã,  Ã, }
Ã,  Ã, Ã,  Ã, i++;
Ã, Ã,  Ã, }
Ã,  }
Ã,  i=0;
Ã,  while (i<6) {
Ã,  Ã,  Display("In position #%d, value %d was %c", i, n[i], nn[i]);
Ã,  Ã,  i++;
Ã,  }
}




As for the bitwsie things. Compare:

(binary values)

1000Ã, &  0001 = 0000
1000 && 0001 = 1
1000 |  0001 = 1001
1000 || 0001 = 1


The logical operators always deal with zero (false) and non-zero (true) and retunr 1 or 0. The bitwise ones do it on every bit individually. If you don't understand this, always use the logical ones.

12

proximity

Ok i got the message. Advanced users only :) Thank you very much.
Proximity Entertainment

SMF spam blocked by CleanTalk