Click here to Skip to main content
13,459,077 members
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 29 Dec 2012

Implementation of an Easy, Fast, and Optimized (CByte, CShort, CInt) with Bits Access Using Bit Field and Union

, 28 Apr 2017
Rate this:
Please Sign up or sign in to vote.
Implementation of an easy, fast, and optimized (CByte, CShort, CInt) with bits access using bit field and union


This article describes bit array classes using bit field and union. This implementation is fast and optimized when compared with the bitset class and is very simple to make complex operations when compared with normal operations.

The total memory size of CByte is only one byte, of CShort is 2 bytes, and CInt is 4 bytes (no extra size).

It's fast and stable in large calculations.

The usage is simple like bitset:

CByte byte1=1;
CByte byte2=9;


Bit Fields: MASN

In addition to declarators for members of a structure or union, a structure declarator can also be a specified number of bits, called a "bit field." Its length is set off from the declarator for the field name by a colon. A bit field is interpreted as an integral type.

The following example declares a structure that contains bit fields:

struct Date
   unsigned nWeekDay  : 3;    // 0..7   (3 bits)
   unsigned nMonthDay : 6;    // 0..31  (6 bits)
   unsigned nMonth    : 5;    // 0..12  (5 bits)
   unsigned nYear     : 8;    // 0..100 (8 bits)

Note that nYear is 8 bits long and would overflow the word boundary of the declared type, unsigned int. Therefore, it is begun at the beginning of a new unsigned int. It is not necessary that all bit fields fit in one object of the underlying type; new units of storage are allocated according to the number of bits requested in the declaration.

Union: MSDN

A union is a user-defined data or class type that, at any given time, contains only one object from its list of members (although that object can be an array or a class type).

// declaring_a_union.cpp
union DATATYPE    // Declare union type
    char   ch;
    int    i;
    long   l;
    float  f;
    double d;
} var1; // Optional declaration of union variable

The member-list of a union represents the kinds of data the union can contain. A union requires enough storage to hold the largest member in its member-list.

A C++ union is a limited form of the class type. It can contain access specifiers (public, protected, private), member data, and member functions, including constructors and destructors. It cannot contain virtual functions or static data members. It cannot be used as a base class, nor can it have base classes. The default access of members in a union is public.

Part of CByte Implementation

union CByte
    unsigned char Value;
    struct {
        unsigned char b0:1;
        unsigned char b1:1;
        unsigned char b2:1;
        unsigned char b3:1;

        unsigned char b4:1;
        unsigned char b5:1;
        unsigned char b6:1;
        unsigned char b7:1;

Value and the bits struct refer to the same memory. When any change is made in the value, it will affect the bits and vice versa.

Using the Code

The code has the implementation for most operator overloadings like +,-,++,--,[ ],....

You can get or set any bit in one step very fast and simply.


Code example:

CByte byte1=1;
CByte byte2=9;
CByte byte3=5;







This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ahmed Elkafrawy
Egypt Egypt
بسم الله الرحمن الرحيم
وَنَزَعۡنَا مَا فِى صُدُورِهِم مِّنۡ غِلٍّ۬ تَجۡرِى مِن تَحۡتِہِمُ ٱلۡأَنۡہَـٰرُ‌ۖ وَقَالُواْ ٱلۡحَمۡدُ لِلَّهِ ٱلَّذِى هَدَٮٰنَا لِهَـٰذَا وَمَا كُنَّا لِنَہۡتَدِىَ لَوۡلَآ أَنۡ هَدَٮٰنَا ٱللَّهُ‌ۖ لَقَدۡ جَآءَتۡ رُسُلُ رَبِّنَا بِٱلۡحَقِّ‌ۖ وَنُودُوٓاْ أَن تِلۡكُمُ ٱلۡجَنَّةُ أُورِثۡتُمُوهَا بِمَا كُنتُمۡ تَعۡمَلُونَ 
صدق الله العظيم

In the name of Allah, the Beneficent, the Merciful
"And We remove whatever rancour may be in their hearts. Rivers flow beneath them. And they say: The praise to Allah, Who hath guided us to this. We could not truly have been led aright if Allah had not guided us. Verily the messengers of our Lord did bring the Truth. And it is cried unto them: This is the Garden. Ye inherit it for what ye used to do. (43)"

You may also be interested in...


Comments and Discussions

QuestionMessage Removed Pin
14-Aug-17 21:35
memberMember 250475814-Aug-17 21:35 
QuestionA few minor nit-picks Pin
Daniel Pfeffer30-Apr-17 0:59
professionalDaniel Pfeffer30-Apr-17 0:59 
AnswerRe: A few minor nit-picks Pin
Ahmed Elkafrawy3-May-17 4:27
memberAhmed Elkafrawy3-May-17 4:27 
SuggestionRe: A few minor nit-picks Pin
L. Braun29-May-17 6:40
memberL. Braun29-May-17 6:40 
GeneralRe: A few minor nit-picks Pin
Ahmed Elkafrawy7-Jun-17 3:00
memberAhmed Elkafrawy7-Jun-17 3:00 
GeneralRe: A few minor nit-picks Pin
L. Braun17-Jul-17 21:35
memberL. Braun17-Jul-17 21:35 
GeneralMissing download Pin
Smitha Vijayan6-Mar-13 5:38
staffSmitha Vijayan6-Mar-13 5:38 
GeneralRe: Missing download Pin
Ahmed Elkafrawy28-Apr-17 0:47
memberAhmed Elkafrawy28-Apr-17 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180323.1 | Last Updated 28 Apr 2017
Article Copyright 2012 by Ahmed Elkafrawy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid