Thread: bitfield "array"

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    bitfield "array"

    how do i make a bitfield array when in theory it is not supposed to exist because pointers just don't work with bitfields
    i know i can try with a struct but can i create a dynamic packed array?

    thx a lot in advance

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> how do i make a bitfield array

    you can't, but an std::bitset is a good substitute.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Eg, you have this private in your class
    unsigned long bitArray[10];

    You overload the [ ] operator for the class to do what you want.
    - extract an array index (i / 32) and a bit position (i % 32)
    - use bitwise operators &,| to extract or insert a bit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Sounds a little tricky there Salem... IMHO I wouldn't mind wasting a little space putting a bit in a char for easy access.

    By the way, I've always been wondering... how much space does a bool take up?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> By the way, I've always been wondering... how much space does a bool take up?

    a byte

    >> Sounds a little tricky

    crude example:

    Code:
    struct bits
    {
    	protected:
    	
    	struct proxy
    	{
    		proxy(unsigned char * ptr, unsigned char index)
    		: base(ptr), mask(1 << index)
    		{
    			
    		}
    		
    		operator bool (void)
    		{
    			return *base & mask;
    		}
    		
    		proxy &
    		operator = (bool state)
    		{
    			*base = state ? *base | mask : *base & ~mask;
    			return *this;
    		}
    		
    		unsigned char * base;
    		unsigned char mask;
    	};
    	
    	public:
    	
    	bits(void)
    	: base(0), limit(0)
    	{
    		
    	}
    	
    	template <class T>
    	bits(T & obj)
    	{
    		set(obj);
    	}	
    	
    	bits(void * ptr, unsigned len)
    	{
    		set(ptr, len);
    	}
    	
    	bits(void * ptr, void * end)
    	{
    		set(ptr, end);
    	}	
    	
    	template <class T>
    	bits &
    	set(T & obj)
    	{
    		return set(&obj, sizeof(obj));
    	}	
    	
    	bits &
    	set(void * ptr, unsigned len)
    	{
    		base = (unsigned char*)ptr;
    		limit = base + len;
    		return *this;
    	}
    	
    	bits &
    	set(void * ptr, void * end)
    	{
    		base = (unsigned char*)ptr;
    		limit = (unsigned char*)end;
    		return *this;
    	}
    	
    	proxy
    	operator [] (unsigned index)
    	{
    		return proxy(&base[index / 8], index % 8);
    	}
    
    	protected:
    	
    	unsigned char * base, * limit;	
    };
    
    int main()
    {
    	int i = 16;
    	bits ib(i);
    	cout << i << endl;
    	ib[1] = 1;
    	cout << i << endl;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    FYI bibsik,

    I don't know about your application, but in the hardware world we work with individual bits a lot. The trick is to use hexadecimal instead of binary. With a little study and practice, you can learn to convert numbers of any size between hex and binary. (...Not so easy with decimal!)

    - In C++, hex is much easier than binary. Hex is built into cin and cout, and you can use it directly in your source code.

    - It's also easier to read, write, and speak... It's a lot easier to say "F5" than "1111 0101". When you get more than 8 bits, binary starts getting really tough.

    how do i make a bitfield array when in theory it is not supposed to exist because pointers just don't work with bitfields
    i know i can try with a struct but can i create a dynamic packed array?
    I'm not sure what you are trying to do... In the computer's memory, an integer is stored in binary.* So, with a 16-bit integer, you can store the "binary state" of 16 different "things". Back in my hardware world... one bit might represent the on-off state of an LED. Another bit might represent the on-off state of a particular switch. If you display that integer in hex or binary, you can easily "see" the state of each "thing". (You can do the same thing if you display the number in decimal, it just takes a little more work to figure-out the individual bit-states.)

    * Of course, everything is stored in binary. The application (and operating system) have to keep track of what the "numbers" at at each memory address are used for. The binary number might simply represent the numeric integer-value (1111 0101 = 245 decimal), or it might represent an ASCII character, or something else... like a machine-language CPU instruction.
    Last edited by DougDbug; 04-12-2006 at 05:06 PM.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    What I meant by "tricky": several computer instructions just to extract one bit. Of course, I could be wrong about that.

    Doug has a good point there
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitfield
    By $l4xklynx in forum C Programming
    Replies: 26
    Last Post: 12-25-2008, 08:52 AM
  2. "Array" of strings
    By Yorae in forum C Programming
    Replies: 5
    Last Post: 02-25-2006, 01:45 PM
  3. bitfield memory question
    By sufthingol in forum C++ Programming
    Replies: 19
    Last Post: 03-26-2005, 04:43 PM