How make “N” bit Integer??

#include <iostream>

using namespace std;

template <int Size>
class NInt
{
private:
int Int:Size;
public:
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;
obj.PrintValue();
obj.SetValue(1);
obj.PrintValue();
obj.SetValue(2);
obj.PrintValue();
obj.SetValue(4);
obj.PrintValue();
obj.SetValue(5);
obj.PrintValue();
cout<<“Size of : “<<sizeof(obj)<<endl;
cout<<“press any key to continue…”<<endl;
_gettch();
return 0;
}

Output
———
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.
struct-declarator:
declarator
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] http://msdn2.microsoft.com/en-us/library/yszfawxh(VS.80).aspx

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s