Core Function SetBit

From Sputnik Wiki
Revision as of 09:07, 11 August 2014 by UberFoX (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
SetBit( <binary-array>, <index>, <value> )

Contents

Description

Set the bit at a given index.

Parameters

binary-array

The binary variable to use.

index

Index of the bit to set (This is not the index of the bytes).

value

Optional; Value to set the bit to. (Should be TRUE or FALSE)

Default: FALSE

Return Value

Success: Returns true if operation was a success (even if the bit was already set to what we wanted to change them to)

Failure: Returns false

Remarks

This is a very good way to store a ton of stuff inside bytes since you could store an array of about 8 individual true/false states in just 1 byte or an array of about 32 individual true/false states in just 4 bytes and so on.

This would use up an incredibly less amount of ram and processing power compared to using a dictionary directly.

See example to learn how to use this.

Example

// Create a binary variable to hold the Bits
// It can be any size we want but for now 4 bytes
// is enough to hold the above privs
$Access = BinaryCreate(4, 0x00);
// Make sure all privs are off
FillBit($Access, false);
 
// Print privs to see default state
// (Should all be disabled)
say "--- Privs ---";
PrintPrivs($Access);
say; // Blank new line
 
// Lets add some Privs
SetBit($Access, Privs->$UploadFile, true);
SetBit($Access, Privs->$CreateFolder, true);
SetBit($Access, Privs->$ReadChat, true);
SetBit($Access, Privs->$CloseChat, true);
 
// Print privs again to see changes
say "--- Privs After Change ---";
say "(UploadFile, CreateFolder, ReadChat and CloseChat will be enabled)";
PrintPrivs($Access);
say; // Blank new line
 
// Lets add some Privs again
SetBit($Access, Privs->$SendChat, true);
// Print privs again to see changes
say "--- Privs After Change (1 added) ---";
say "(in addition to above SendChat will be enabled)";
PrintPrivs($Access);
say; // Blank new line
 
// Lets remove some Privs
SetBit($Access, Privs->$SendChat, false);
SetBit($Access, Privs->$CreateFolder, false);
// Print privs again to see changes
say "--- Privs After Change (2 removed) ---";
say "(SendChat and CreateFolder will be removed)";
PrintPrivs($Access);
say; // Blank new line
 
// Now lets just turn all privs on
FillBit($Access, true);
say "--- Privs After Change ---";
say "(All Enabled)";
PrintPrivs($Access);
say; // Blank new line
 
// Finally lets just turn all privs off
FillBit($Access, false);
say "--- Privs After Change ---";
say "(All Disabled)";
PrintPrivs($Access);
say; // Blank new line
 
// This function will print all the privs that
// are available to us every time its called
Function PrintPrivs( $data )
{
	my $Allowed = 0;
	foreach( Enumerate('Privs') as my $Key => my $Value )
	{
		my $Status = GetBit($data, $Value);
		if($Status) // Only show privs we can use
		{
			say "Priv '$Key' Access is '$Status'";
			$Allowed++;
		}
	}
	if(!$Allowed)
	{
		say "You have no privs";
	}
}
 
// Prints
//
// --- Privs ---
// You have no privs
//
// --- Privs After Change ---
// (UploadFile, CreateFolder, ReadChat and CloseChat will be enabled)
// Priv 'uploadfile' Access is 'true'
// Priv 'createfolder' Access is 'true'
// Priv 'readchat' Access is 'true'
// Priv 'closechat' Access is 'true'
//
// --- Privs After Change (1 added) ---
// (in addition to above SendChat will be enabled)
// Priv 'uploadfile' Access is 'true'
// Priv 'createfolder' Access is 'true'
// Priv 'readchat' Access is 'true'
// Priv 'sendchat' Access is 'true'
// Priv 'closechat' Access is 'true'
//
// --- Privs After Change (2 removed) ---
// (SendChat and CreateFolder will be removed)
// Priv 'uploadfile' Access is 'true'
// Priv 'readchat' Access is 'true'
// Priv 'closechat' Access is 'true'
//
// --- Privs After Change ---
// (All Enabled)
// Priv 'deletefile' Access is 'true'
// Priv 'uploadfile' Access is 'true'
// Priv 'downloadfile' Access is 'true'
// Priv 'renamefile' Access is 'true'
// Priv 'movefile' Access is 'true'
// Priv 'createfolder' Access is 'true'
// Priv 'deletefolder' Access is 'true'
// Priv 'renamefolder' Access is 'true'
// Priv 'movefolder' Access is 'true'
// Priv 'readchat' Access is 'true'
// Priv 'sendchat' Access is 'true'
// Priv 'createchat' Access is 'true'
// Priv 'closechat' Access is 'true'
//
// --- Privs After Change ---
// (All Disabled)
// You have no privs
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox