BITWISE STORAGE
namespace BitManipulation{
public class BitManipulation
{
public int[] bitset;
public BitManipulation(int size)
{
//bitset = new int[size >> 5]; //divide by 32
bitset = new int[size / 32];
}
public Boolean get(int number)
{
//int actualnumber = (number >> 5); //divide by 32
//int bitnumber = (number & 0x1F);
int actualnumber = (number / 32); //divide by 32
int bitnumber = (number % 32);
return (bitset[actualnumber] & (1 << bitnumber)) != 0;
}
void set(int number)
{
//int actualnumber = (number >> 5); // divide by 32
//int bitnumber = (number & 0x1F); // mod 32
int actualnumber = (number / 32); // divide by 32
int bitnumber = (number % 32); // mod 32
bitset[actualnumber] = bitset[actualnumber] | 1 << bitnumber;
}
public static void Main(String[] args) {
//memory = 4KB = 4 * 8 = 32k = size
BitManipulation obj = new BitManipulation(32000);
obj.set(100);
obj.set(0);
obj.set(1);
obj.set(2);
obj.set(3);
if(obj.get(1))
Console.WriteLine("1 is present");
if(obj.get(3))
Console.WriteLine("3 is present");
if(obj.get(5))
{
}
else
Console.WriteLine("5 is not present");
obj.set(5);
obj.set(10);
if(obj.get(5))
Console.WriteLine("5 is present");
ArrayList list = new ArrayList();
}
}
}
//To store negative number you can have a map function which maps a negative number to a positive arr index and bit number.
No comments:
Post a Comment