Advertisement
Java_Volume1 DDE #99975

Get a bit, extract a value, or set a bit from a number

These two macros will get/modify the bit in a number according to a position counted from right to left. For example, let's say we had 1101 1111 (223). We would just use my macro to get the 3rd bit from the right which would be a one. We can also modify it to a 0. Or we want to extract the value of the first four bits, all can be done easily!

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
THIS FILE SHOULD BE NAMED (ircBot.php)
<?	
    // this is the base class. do NOT 
    // instatiate this. you need to write
    // a class to derive from it and implement
    // a constructor and override all of the
    // on_ functions. An example of a derived
    // runnable bot is listed beneath.
	class IRC_Bot {
  	var $nick; 
  	var $username;		
		var $description;
		var $localhost;
		var $remotehost;
		var $remoteport;
		var $echoincoming;
		var $ircsocket;
		function IRC_Bot() {  
	    set_time_limit(0);
			ob_end_flush();
			echo "\r\n";
		}
		function bot_connect () {
			// connect to IRC server
			$this->ircsocket = fsockopen ($this->remotehost, $this->remoteport) ;
			if (! $this->ircsocket) {
				die ("Error connecting to host.");
			}
			print "Connected to: $this->remotehost:$this->remoteport\n";
			fputs ($this->ircsocket, "USER $this->username $this->localhost $this->remotehost: $this->description\r\n");
			fputs ($this->ircsocket, "NICK $this->nick\r\n");
		}
		function bot_go () {
			// IRC loop
			while (!feof($this->ircsocket)) {
				$incoming = fgets ($this->ircsocket, 1024);
				$incoming = str_replace( "\r", "", $incoming);
				$incoming = str_replace("\n", "", $incoming);
				if ($this->echoincoming) echo $incoming . "\n";
				if (substr($incoming, 0, 1) == ":") {
					$prefix = substr ($incoming, 0, strpos($incoming, ' ')); 
					$incoming = substr ($incoming, strpos($incoming, ' ') + 1);
				} else {
					$prefix = "";
				}
				$command = substr ($incoming, 0, strpos($incoming, ' '));
				$incoming = substr ($incoming, strpos($incoming, ' ') + 1);
				$params = explode (" ", $incoming);
				if ($command == "PING") fputs($this->ircsocket, "PONG\r\n");
				$this->bot_parse ($prefix, $command, $params);
			}		
			fputs($this->ircsocket, "QUIT Unexpected\r\n");
		}
		
		function bot_parse ($prefix, $command, $params) {
			if ($command == "PRIVMSG") {
				$nick = substr ($prefix, strpos($prefix, ":") + 1, strpos($prefix, "!") - 1);
				$ident = substr ($prefix, strpos($prefix, "!"));
				$target = array_shift ($params);
				$params[0] = substr ($params[0], 1);
				if (substr($target, 0, 1) == "#") {
					$this->on_channel_msg ($nick, $ident, $target, $params);
				} else {				
					$this->on_private_msg ($nick, $ident, $params);
				}
			}
			if ($command == "NOTICE") {
				$nick = substr ($prefix, strpos($prefix, ":") + 1, strpos($prefix, "!") - 1);
				$ident = substr ($prefix, strpos($prefix, "!"));
				array_shift ($params);
				$params[0] = substr ($params[0], 1);
				$this->on_notice ($nick, $ident, $params);
			}
		}

		////////////////////////////////////////////////////
		//				IRC FUNCTIONS (call these to perform various irc tasks.)	 //
		////////////////////////////////////////////////////
		function irc_write ($message) {
			fputs ($this->ircsocket, $message . "\r\n");
		}
		
		function irc_join ($channel) {
			$this->irc_write("JOIN $channel");
		}
		function irc_part($channel) {
			$this->irc_write("PART $channel");
		}
		function irc_quit ($reason) {
			$this->irc_write("QUIT :$reason");
		}
		function irc_notice ($user, $message) {
			$this->irc_write("NOTICE :$message");
		}
		
		function irc_msg ($user, $message) {
			$this->irc_write("PRIVMSG $user :$message");
		}
		function irc_action ($user, $message) {
			$this->irc_write ("PRIVMSG $user :" . chr(1) ."ACTION $message");
		}		
		function irc_mode ($channel, $user, $mode) {
			$this->irc_write ("MODE $channel $mode $user");
		}
		function irc_op ($channel, $user) {
			$this->irc_mode ($channel, $user, "+o");
		}
		function irc_deop ($channel, $user) {
			$this->irc_mode ($channel, $user, "-o");
		}

		
		////////////////////////////////////////////////////
		//				IRC EVENTS (override these in your derived class.)				 //
		////////////////////////////////////////////////////
		
		function on_private_msg ($nick, $ident, $params) {
		}
		
		function on_channel_msg ($nick, $ident, $chan, $params) {
		}		
		function on_notice ($nick, $ident, $params) {
		}
		
		
	}
 ?>




PUT ALL THIS IN A DIFFERENT FILE (silverbot.php):
<?
    // this is an example of a runnable 
    // derived bot. run this file at the
    // commandline. DO NOT run it as a
    // web document. it will hang in memory
    // you have been warned.
	define ("BOT_PASSWORD", "fruitloops");
	include ("ircbot.php");

	class Silver_Bot extends IRC_Bot {
		function Silver_Bot ($n = "HBSilver", $r = "irc.dal.net", $p = 6667, $e = true, $d = "Hobbit Bot Silver", $u = "HBSilver", $l = "localhost") {
			$this->IRC_Bot();
			$this->nick = $n;
			$this->username = $u;
			$this->description = $d;
			$this->localhost = $l;
			$this->remotehost = $r;
			$this->remoteport = $p;
			$this->echoincoming = $e;
		}
		
		function on_notice ($nick, $ident, $params) {
			$password = array_shift ($params);
			if ($password == BOT_PASSWORD) {
				$command = array_shift ($params);
				switch ($command) {
				case "JOIN":
					$this->irc_join ($params[0]);
					break;
				case "PART":
					$this->irc_part ($params[0]);
					break;
				case "QUIT":
					$this->irc_quit (join($params, " "));
					break;
				case "MSG":
					$user = array_shift ($params);
					$this->irc_msg($user, join($params, " "));
					break;
				case "OP":
					$this->irc_op($params[0], $params[1]);
					break;
				case "DEOP":
					$this->irc_deop($params[0], $params[1]);
					break;
				case "ACTION":
					$user = array_shift ($params);
					$this->irc_action($user, join($params, " "));
					break;
				}
			} else {
				$this->irc_msg($nick, "You are not my master.");
			}
		}
	}
	// this instantiates a new silverbot
    // and gets it going. 
	$mysilver = new Silver_Bot("HBSilver", "irc.dal.net");
	echo $mysilver->bot_connect();
	echo $mysilver->bot_go();
?>
/*Bit Getting/Setting*/
//Macros//
#define EXTRACT_BITS_RL(the_val, bits_start, bits_len) ((the_val >> (bits_start - 1)) & ((1 << bits_len) - 1))
#define MODIFY_BIT_RL(the_val, bit_num, bit_val) (bit_val == 0 ? (the_val & (~(1 << (bit_num - 1)))) : (the_val | (1 << (bit_num - 1))))
//End Macros//
//Globals//
int iVal = 0, iShift = 0, iBitVal = 0, iMenu = 1;
//End Globals//
//Prototyes//
void ShowVal();
void ShowMenu();
//End Prototypes//
int main()
	{
		while(iMenu < 4) //Better way of avoiding GOTOs
			{
				if(iMenu == 1)
					{
						cout << "Input Test Value (0 to 255): ";
						cin >> iVal;
					}
				if(iMenu == 0)
					{
						cout << "Enter Bit Position To Be Modified (1 to 8): ";
						cin >> iShift;
						cout << "Enter New Bit Value (0 or 1): ";
						cin >> iBitVal;
						iVal = MODIFY_BIT_RL(iVal, iShift, iBitVal); //Modify the number's bits
					}
				if(iMenu == 2)
					{
						ShowVal();
					}
				if(iMenu == 3)
					{
						cout << "Enter Start: ";
						cin >> iShift;
						cout << "Enter Length: ";
						cin >> iBitVal;
						cout << "Extracted Value: " << EXTRACT_BITS_RL(iVal, iShift, iBitVal) << "\n";
					}
				ShowMenu();
			}
	return(0);
	}
void ShowVal()
	{
		cout << "Binary Values (Right To Left) for \"" << iVal << "\"\n";
		for(int I = 1; I < 9; I++)
			{
				cout << I << ": " << EXTRACT_BITS_RL(iVal, I, 1) << "\n"; //Show bit position (Right to Left)
			}
	}
void ShowMenu()
	{
		//Display menu
		cout << "\n->Program Menu:\n0: Modify \"" << iVal << "\"\n1: Choose New Number\n2: Show Values for \"" << iVal << "\"\n3: Extract Value\n4: Quit\nChoose: ";
		cin >> iMenu;
		cout << "\n";
	}
Original Comments (3)
Recovered from Wayback Machine