473,549 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Number of bits and size of data types in bytes

I just wrote the following and have a question: Why does my code gets
it wrong with the class Simple? See last show_size<Simpl e> 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::ostream& operator<<(std: :ostream& ostream, Simple&
simple);

private:
std::string t;
};

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

Simple::~Simple ()
{
}

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

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

template <typename T>
const int get_n_bits()
{
return std::numeric_li mits<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::ostream& show_size(std:: ostream& stream, const char* type)
{
stream << type << " has ";
stream << get_n_bits<T>() << " bits";
stream << ", has length of ";
stream << sizeof(T);
stream << " ";
stream << pluralise<T>("b yte", "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<doubl e>(std::cout, "double");
show_size<long double>(std::co ut, "long double");
show_size<wchar _t>(std::cout, "wchar_t");
show_size<Simpl e>(std::cout, "Simple");

return 0;
}
--
http://www.munted.org.uk

Take a nap, it saves lives.
Jun 9 '06 #1
9 2084
Alex Buell <al********@mun ted.org.uk> 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)
Jun 9 '06 #2
On Fri, 9 Jun 2006 13:59:28 +0200, I waved a wand and this message
magically appeared from Jakob Bieling:
Alex Buell <al********@mun ted.org.uk> 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.
Jun 9 '06 #3
Alex Buell <al********@mun ted.org.uk> wrote:
On Fri, 9 Jun 2006 13:59:28 +0200, I waved a wand and this message
magically appeared from Jakob Bieling:
Alex Buell <al********@mun ted.org.uk> 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)
Jun 9 '06 #4
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 <al********@mun ted.org.uk> 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
Jun 9 '06 #5
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.
Jun 9 '06 #6
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.
Jun 9 '06 #7

"Alex Buell" <al********@mun ted.org.uk> wrote in message

Take a nap, it saves lives.


Except while driving.
Jun 9 '06 #8
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.
Jun 9 '06 #9
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.

Jun 9 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
43001
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number (or would HDD be better, or both?) and put that info into VB prog so the program won't work on another computer. My program uses an MSAccess table....
36
8353
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N, but a prime number tester would also be nice. I'm dealing with numbers in the 10^6-10^8 range so it would have to fairly efficient Dag
10
3254
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not 'sizeof(char)'. It seems that I could use bitset<8> to represent a byte in my code --- if you have a better suggestion, I welcome it --- but that...
70
6185
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not...
11
2545
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
14
11909
by: ern | last post by:
Does a function exist to convert a 128-bit hex number to a string?
25
4532
by: Daniel Kraft | last post by:
Hi, I do need to implement something similar to C++'s std::bitset in C; for this, I use an array of int's to get together any desired number of bits, possibly larger than 32/64 or anything like this. So of course it does not matter how many bits a single int has, but I do need a reliable way to find it out. I think of something like
16
2874
by: Dom Fulton | last post by:
Has anyone got a mechanism for finding the number of bits in floats, doubles, and long doubles? I need this to communicate with some hardware. I guess I could try to deduce this from float.h, but that looks difficult. I don't mind something which is system-specific; the older gccs had various defines in cpu_limits.h and related files,...
9
5153
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte level parsing what ever may be the type of data.The data would be coming from an external environment.When I parse int,char at byte level,I get right...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7959
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5369
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.