How make “N” bit Integer??

#include <iostream>

using namespace std;

template <int Size>
class NInt
int Int:Size;
int GetValue();
void SetValue(int value);
void PrintValue();

template <int Size>
int NInt<Size>::GetValue()
return this->Int;

template <int Size>
void NInt<Size>::SetValue(int value)
this->Int = value;

template <int Size>
void NInt<Size>::PrintValue()
cout<<“Value : “<<this->Int<<endl;

int _tmain(int argc, _TCHAR* argv[])
NInt<2> obj;
cout<<“Size of : “<<sizeof(obj)<<endl;
cout<<“press any key to continue…”<<endl;
return 0;

Value : 0
Value : 1
Value : -2
Value : 0
Value : 1
Size of : 4
press any key to continue…

C Bit Fields
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.
type-specifier declarator opt : constant-expression

The constant-expression specifies the width of the field in bits. The type-specifier for the declarator must be unsigned int, signed int, or int, and the constant-expression must be a nonnegative integer value. If the value is zero, the declaration has no declarator. Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed. The optional declarator names the bit field. Bit fields can only be declared as part of a structure. The address-of operator (&) cannot be applied to bit-field components.
Unnamed bit fields cannot be referenced, and their contents at run time are unpredictable. They can be used as “dummy” fields, for alignment purposes. An unnamed bit field whose width is specified as 0 guarantees that storage for the member following it in the struct-declaration-list begins on an int boundary.
Bit fields must also be long enough to contain the bit pattern. For example, these two statements are not legal:
short a:17; /* Illegal! */
int long y:33; /* Illegal! */
[Reference link]


One thought on “How make “N” bit Integer??

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s