/* --- bit --- turn bits on and off and like (c) 5.5.1995 Mika V„liviita freeware, use as you wish at your own risk my bbbs is evaluate! +358-30-5455104 V32B */ /* Something you should be aware of: ================================= say we have a number... decimal: 85 hex: 55 binary: 01010101 <- the rightmost number is counted as bit 0 not 1! ^ the leftmost number is counted here as bit 7 let's have an example: 01010101 (= 85) ^ this is bit 1 now we turn bit 1 ON, and here is the result: 01010111 (= 87) and if you use above functions for it, do it like this: printf("85 and 2^1 = %u",biton(1,85)); easy! */ // implementation of fex 2^5 // couldn't find it in scipt.gui // so did it myself. // no values checked function pow($bas,$pow) { var $foo,$bar; $bar=$bas; switch($pow) { case 0: { $foo=1; break; } case 1: { $foo=$bas; break; } default: { for ($foo=0; $foo < $pow-1; $foo=$foo+1) { $bar=$bar*$bas; } $foo=$bar; break; } } // printf(" þ%uþ ",$foo); return($foo); } // turn bit $bit ON in number $in function biton($bit,$in) { return($in | pow(2,$bit)); } // turn bit $bit OFF in number $in function bitoff($bit,$in) { return($in & (-pow(2,$bit)-1)); } // toggle bit $bit in number $in function bittgl($bit,$in) { return($in ^ pow(2,$bit)); } function main() { printf("\n"); printf("\e[0;1m Demonstration of functionality:\e[0m\n"); printf("\n"); printf(" biton(4,11) = %u\n",biton(4,11)); printf(" biton(1,11) = %u\n",biton(1,11)); printf("\n"); printf(" bitoff(1,11) = %u\n",bitoff(1,11)); printf(" bitoff(2,11) = %u\n",bitoff(2,11)); printf("\n"); printf(" bittgl(2,11) = %u\n",bittgl(2,11)); printf(" bittgl(1,11) = %u\n",bittgl(1,11)); printf("\n"); printf(" pow(2,16) = %u\n",pow(2,16)); printf(" pow(5,6) = %u\n",pow(5,6)); printf("\n"); }