Thread: Bit utility - Need testing...

Results 1 to 1 of 1
  1. #1
    begadang jangan begadang =_= anggara_nothing's Avatar
    Join Date
    Jan 2016
    Location
    Indonesia
    Posts
    155

    Explosive Crate Bit utility - Need testing...

    In console, no error output.
    Is this on good shape?

    Code:
    /*
    	    Bit functions
    	
    	  by  Lulu the hero
    	(m_lajos@hotmail.com)
    
    https://forums.alliedmods.net/showthread.php?t=144645
    	
    Last update(dd.mm.yyyy): 03.03.2011.
    
    These set of stocks/defines ease up bit manipulations.
    
    This file is provided as is (no warranties).
    */
    
    const int cellbits = 32;
    const int cellnibbles = 8;
    
    enum BitCountType
    {
    	COUNT_BIT = 1,
    	COUNT_NIBBLE = 4
    };
    
    const string HEXCHARS = "0123456789ABCDEF";
    
    int player_to_flag( const int id )
    {
    	return (1 << (id & 31));
    }
    
    int bit_to_flag( const int bit )
    {
    	return (1 << bit);
    }
    
    /* ----- in the functions below %1 is the integer, %2 is the player's id/bitnumber ----- */
    
    // Negate a bit - with XOR
    void toggle_player_flag(int &out value, const int playerId)
    {
    	(value = value ^ player_to_flag(playerId));
    }
    void toggle_flag(int &out value, const int playerId)
    {
    	(value = value ^ bit_to_flag(playerId));
    }
    
    // Clear a bit - set it to 0 with NAND
    void clear_player_flag(int &out value, const int playerId)
    {
    	(value = value & ~player_to_flag(playerId));
    }
    void clear_flag(int &out value, const int playerId)
    {
    	(value = value & ~bit_to_flag(playerId));
    }
    
    // Set a bit - set is to 1 with OR
    void set_player_flag(int &out value, const int playerId)		
    {
    	(value = value | player_to_flag(playerId));
    }
    void set_flag(int &out value, const int playerId)
    {
    	(value = value | bit_to_flag(playerId));
    }
    
    // Returns a bit's value - with AND
    bool is_player_flag_set( const int value, const int playerId)
    {
    	return (value & player_to_flag(playerId)) != 0;
    }
    bool is_flag_set( const int value, const int playerId)
    {
    	return (value & bit_to_flag(playerId)) != 0;
    }
    
    // Converts the integer to a binary number string
    // ( max string length is cellbits )
    uint num_to_binstr(const int num, string &out dest, bool zero_fill = false)
    {
    	dest[0] = 0;
    	uint len = (zero_fill ? cellbits : get_highest(num, COUNT_BIT));
    	for(uint i = 0; i < len; i++)
    	{
    		dest[len - i] = "" + ((num >> i) & 1) + '0';
    	}
    	len++;
    	dest[len] = 0;
    	return len;
    }
    
    // Converts the integer to a hexadecimal number string
    // ( max string length is cellnibbles )
    uint num_to_hexstr(const int num, string &out dest, bool zero_fill = false)
    {
    	dest[0] = 0;
    	uint len = (zero_fill ? cellnibbles : get_highest(num, COUNT_NIBBLE));
    	for(uint i = 0; i < len; i++)
    	{
    		dest[len - i] = HEXCHARS[((num >> (i << 2)) & 15)];
    	}
    	len++;
    	dest[len] = 0;
    	return len;
    }
    
    // Rotates an integer's bits to left
    int rol(int &in integer, int &in step = 1)
    {
    	if(step >= cellbits)
    	{
    		step = step % cellbits;
    	}
    	if(step < 1)
    	{
    		return integer;
    	}
    	int overflow = integer;
    	integer <<= step;
    	overflow >>= (cellbits - step);
    	return integer | overflow;
    }
    
    // Rotates an integer's bits to right
    int ror(int &in integer, int &in step = 1)
    {
    	if(step >= cellbits)
    	{
    		step = step % cellbits;
    	}
    	if(step < 1)
    	{
    		return integer;
    	}
    	int overflow = integer;
    	integer >>= step;
    	overflow <<= (cellbits - step);
    	return integer | overflow;
    }
    
    // Gets the highest bit/nibble
    int get_highest(int &in integer, const BitCountType count_type = COUNT_BIT)
    {
    	int num = 0;
    	if(integer > 0) num++;
    	while ( (integer >>= count_type) > 0)
    	{
    		num++;
    	}
    	return num;
    }
    
    // Counts the 1 bits in a number
    uint count_bits(int &in integer)
    {
    	uint cnt;
    	for(cnt = 0; integer > 0; cnt++)
    	{
    		integer &= integer - 1;
    	}
    	return cnt;
    }
    Last edited by anggara_nothing; 09-08-2017 at 02:47 PM. Reason: Add &in syntax

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •