Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #ifndef HEADER_BITBOOL
  2.   #define HEADER_BITBOOL
  3.   #include "Arduino.h"
  4.  
  5.   template< size_t _Count, bool _Reverse = false >
  6.     class BitBool{
  7.       protected:
  8.         struct BitRef{
  9.        
  10.           BitRef( uint8_t &u_DataRef, const uint8_t u_Idx ) : u_Data( u_DataRef ), u_Index( _Reverse ? ( 0x80 >> u_Idx ) : ( 0x1 << u_Idx ) ) { return; }
  11.  
  12.           operator const bool() const { return this->u_Data & this->u_Index; }
  13.          
  14.           const bool operator =( const BitRef &b_Copy ) const { return *this = ( const bool ) b_Copy; }
  15.  
  16.           const bool operator =( const bool &b_Copy ) const
  17.             {
  18.               if( b_Copy )  this->u_Data |= this->u_Index;
  19.               else          this->u_Data &= ~this->u_Index;
  20.               return b_Copy;
  21.             }      
  22.            
  23.           uint8_t &u_Data;
  24.           uint8_t const u_Index;
  25.         };  
  26.       public:
  27.         enum{ BitCount = _Count, ByteCount = ( BitCount / 0x8 ) + ( ( BitCount % 0x8 ) ? 0x1 : 0x0 ) };
  28.  
  29.         BitRef operator []( const uint16_t i_Index ) { return BitRef( this->Data[ i_Index >> 0x3 ], i_Index & 0x7 ); }  
  30.         uint8_t Data[ ByteCount ];
  31.   };
  32. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement