Custom bit widths

B

blakaxe

How do i create basic datatypes with custom bit widths?

Example: I need a 20 bit signed integer.

I am modeling hardware that can have custom bit widths. How do I do it
in C++?
 
L

Lionel B

How do i create basic datatypes with custom bit widths?

Example: I need a 20 bit signed integer.

I am modeling hardware that can have custom bit widths. How do I do it
in C++?

Depends what you want to do with them, really. If all you need are, say,
the basic bitwise operators and shifts, then std::bitset might work for
you. The bit width is a non-type template argument and thus a compile-
time constant. boost::dynamic_bitset provides similar functionality but
with run-time specifiable number of bits. But they will not handle
arithmetic (signed or otherwise); you would have to implement that
yourself.
 
V

Victor Bazarov

Lionel said:
Depends what you want to do with them, really. If all you need are, say,
the basic bitwise operators and shifts, then std::bitset might work for
you. The bit width is a non-type template argument and thus a compile-
time constant. boost::dynamic_bitset provides similar functionality but
with run-time specifiable number of bits. But they will not handle
arithmetic (signed or otherwise); you would have to implement that
yourself.

What about bit fields? You can't create a reference to one or a pointer
to one, so you'll always have to use the . notation (or the -> notation)
to access the field, but it's supposed to perform just like a built-in
type otherwise, no?

V
 
Z

ZikO

How do i create basic datatypes with custom bit widths?

Example: I need a 20 bit signed integer.

I am modeling hardware that can have custom bit widths. How do I do it
in C++?
There is a bitset<N> template in the standard library (actually it
doesn't belong to the standard library but is ditributed with the STL)
which can store number of bits specified by you. You might be interested
in that ... <bitset>

std::bitset<20> store_bits; // 20 bits to be stored in store_bits;

It also has many useful function helping to operate on single bits,
access to particular bits (operator[]), manipulate, inverse etc. return
as a unsigned int or std::string etc. Construct object by providing bits
as a std::string or unsigned number etc.

Regards.
 
U

Uri Nix

How do i create basic datatypes with custom bit widths?

Example: I need a 20 bit signed integer.

I am modeling hardware that can have custom bit widths. How do I do it
in C++?

Try looking at SystemC, which is actually C++ with hardware modeling
capabilities.
Check the available datatypes, sc_int<20> might do the trick.

Cheers,
Uri
 
L

Lionel B

What about bit fields? You can't create a reference to one or a pointer
to one, so you'll always have to use the . notation (or the -> notation)
to access the field, but it's supposed to perform just like a built-in
type otherwise, no?

Interesting... I don't know to what extent they behave like the
corresponding (integer) type (can you do signed arithmetic with them? do
they behave as expected under shifts? etc.) - don't have a copy of the
standard handy here...

For more convenient access I guess you could wrap them...

template<typename IntType, unsigned N>
struct xint
{
IntType bits:N;

xint(IntType n) : bits(n) {} // non-explicit for conversions

...
};

for starters, then fill in arithmetic & bitwise ops, etc... (don't Boost
do anything like this?)
 
R

red floyd

There is a bitset<N> template in the standard library (actually it
doesn't belong to the standard library but is ditributed with the STL)
which can store number of bits specified by you. You might be interested
in that ... <bitset>

std::bitset<20> store_bits; // 20 bits to be stored in store_bits;

Wrong. std::bitset is part of the Standard Library -- see 23.3.5.

However, it may not necessarily be part of the STL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,428
Latest member
RosalieQui

Latest Threads

Top