Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Number of bits and size of data types in bytes

Reply
Thread Tools

Number of bits and size of data types in bytes

 
 
Alex Buell
Guest
Posts: n/a
 
      06-09-2006
I just wrote the following and have a question: Why does my code gets
it wrong with the class Simple? See last show_size<Simple> function call
in main () as below:

#include <iostream>
#include <string>
#include <limits>

class Simple
{
public:
Simple();
~Simple();

void set(std::string text);
std::string get(void);

friend std:stream& operator<<(std:stream& ostream, Simple&
simple);

private:
std::string t;
};

Simple::Simple() :
t("")
{
}

Simple::~Simple()
{
}

void Simple::set(std::string text)
{
t = text;
}

std::string Simple::get(void)
{
return t;
}

template <typename T>
const int get_n_bits()
{
return std::numeric_limits<T>::digits + 1;
}

template <typename T>
const char* pluralise(const char* singluar, const char* plural)
{
return (sizeof(T) == 1) ? singluar : plural;
}

template <typename T>
std:stream& show_size(std:stream& stream, const char* type)
{
stream << type << " has ";
stream << get_n_bits<T>() << " bits";
stream << ", has length of ";
stream << sizeof(T);
stream << " ";
stream << pluralise<T>("byte", "bytes");
stream << ".\n";

return stream;
}

int main(int argc, char *argv[])
{
show_size<bool>(std::cout, "bool");
show_size<char>(std::cout, "char");
show_size<short int>(std::cout, "short int");
show_size<int>(std::cout, "int");
show_size<long int>(std::cout, "long int");
show_size<long long>(std::cout, "long long");
show_size<float>(std::cout, "float");
show_size<double>(std::cout, "double");
show_size<long double>(std::cout, "long double");
show_size<wchar_t>(std::cout, "wchar_t");
show_size<Simple>(std::cout, "Simple");

return 0;
}


--
http://www.munted.org.uk

Take a nap, it saves lives.
 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      06-09-2006
Alex Buell <> wrote:
> I just wrote the following and have a question: Why does my code gets
> it wrong with the class Simple?


What do you mean "gets it wrong"? What do you get and what do you
expect?

regards
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
 
 
 
Alex Buell
Guest
Posts: n/a
 
      06-09-2006
On Fri, 9 Jun 2006 13:59:28 +0200, I waved a wand and this message
magically appeared from Jakob Bieling:

> Alex Buell <> wrote:
> > I just wrote the following and have a question: Why does my code
> > gets it wrong with the class Simple?

>
> What do you mean "gets it wrong"? What do you get and what do you
> expect?


I got the result:
Simple has 1 bits, has length of 4 bytes.

This can't possibly be right? I'd have expected something bigger? Or
am I missing something?
--
http://www.munted.org.uk

Take a nap, it saves lives.
 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      06-09-2006
Alex Buell <> wrote:

> On Fri, 9 Jun 2006 13:59:28 +0200, I waved a wand and this message
> magically appeared from Jakob Bieling:


>> Alex Buell <> wrote:


>>> I just wrote the following and have a question: Why does my code
>>> gets it wrong with the class Simple?


>> What do you mean "gets it wrong"? What do you get and what do you
>> expect?


> I got the result:
> Simple has 1 bits, has length of 4 bytes.


> This can't possibly be right? I'd have expected something bigger? Or
> am I missing something?


Your implementation for std::string is probably using just a pointer
(and I assume a pointer is 4 bytes on your platform) to store the
string, which is why your Simple class (which just contains a string) is
4 bytes long.

The "1 bit" info is a bit misleading tho. "numeric_limits" is not
meaningful in your case, because there is no specialization for this
type. It would not make much sense either, because your class does not
represent a numeric value, so there are no digits to be stored.

Your function returns 1, because numeric_limits (the unspecialized
template) has 0/false for all its members .. and you add 1 to that ..

hth
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-09-2006
Alex Buell wrote:
> On Fri, 9 Jun 2006 13:59:28 +0200, I waved a wand and this message
> magically appeared from Jakob Bieling:
>
>> Alex Buell <> wrote:
>>> I just wrote the following and have a question: Why does my code
>>> gets it wrong with the class Simple?

>>
>> What do you mean "gets it wrong"? What do you get and what do you
>> expect?

>
> I got the result:
> Simple has 1 bits, has length of 4 bytes.
>
> This can't possibly be right? I'd have expected something bigger? Or
> am I missing something?


Nothing besides the fact that 'numeric_limits' is for arithmetic types
mostly, and to have it work for you it has to be _specialised_, which
you don't seem to have done.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Alex Buell
Guest
Posts: n/a
 
      06-09-2006
On Fri, 9 Jun 2006 09:54:17 -0400, I waved a wand and this message
magically appeared from Victor Bazarov:

> > This can't possibly be right? I'd have expected something bigger? Or
> > am I missing something?

>
> Nothing besides the fact that 'numeric_limits' is for arithmetic types
> mostly, and to have it work for you it has to be _specialised_, which
> you don't seem to have done.


Ah, I need to do that. Thanks.
--
http://www.munted.org.uk

Take a nap, it saves lives.
 
Reply With Quote
 
Alex Buell
Guest
Posts: n/a
 
      06-09-2006
On Fri, 9 Jun 2006 15:52:57 +0200, I waved a wand and this message
magically appeared from Jakob Bieling:

> > This can't possibly be right? I'd have expected something bigger? Or
> > am I missing something?

>
> Your implementation for std::string is probably using just a
> pointer (and I assume a pointer is 4 bytes on your platform) to store
> the string, which is why your Simple class (which just contains a
> string) is 4 bytes long.
>
> The "1 bit" info is a bit misleading tho. "numeric_limits" is not
> meaningful in your case, because there is no specialization for this
> type. It would not make much sense either, because your class does
> not represent a numeric value, so there are no digits to be stored.
>
> Your function returns 1, because numeric_limits (the
> unspecialized template) has 0/false for all its members .. and you
> add 1 to that ..


You're absolutely right, I'll change it so it returns 0 for non numeric
types.
--
http://www.munted.org.uk

Take a nap, it saves lives.
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      06-09-2006

"Alex Buell" <> wrote in message
>
> Take a nap, it saves lives.


Except while driving.


 
Reply With Quote
 
Alex Buell
Guest
Posts: n/a
 
      06-09-2006
On Fri, 09 Jun 2006 14:35:04 GMT, I waved a wand and this message
magically appeared from Howard:

> > Take a nap, it saves lives.

>
> Except while driving.


Wiseguy.
--
http://www.munted.org.uk

Take a nap, it saves lives.
 
Reply With Quote
 
Markus Schoder
Guest
Posts: n/a
 
      06-09-2006
Alex Buell wrote:
> On Fri, 9 Jun 2006 09:54:17 -0400, I waved a wand and this message
> magically appeared from Victor Bazarov:
>
> > > This can't possibly be right? I'd have expected something bigger? Or
> > > am I missing something?

> >
> > Nothing besides the fact that 'numeric_limits' is for arithmetic types
> > mostly, and to have it work for you it has to be _specialised_, which
> > you don't seem to have done.

>
> Ah, I need to do that. Thanks.


Rather use

CHAR_BIT * sizeof(T)

if you want the size in bits where CHAR_BIT is defined in <climits>.

numeric_limits<T>::digits

gives the number of digits in terms of the internally used base which
needs not be 2 for float types. It also excludes the sign bit for
integers and gives only the mantissa digits for float types.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could a struct with size 44 bytes point always points to a char array with size 2024 bytes? eagle_jyjh@citiz.net C++ 8 04-10-2006 03:05 PM
Could a struct with size 44 bytes point always points to a char array with size 2048 bytes? eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM
what about unsigned and signed 8 bits number, 16 bits, etc?? sarmin kho Python 2 06-15-2004 06:40 PM
Re: what about unsigned and signed 8 bits number, 16 bits, etc?? Miki Tebeka Python 1 06-14-2004 03:19 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM



Advertisments