Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

representation of a number (bytes)

44 views
Skip to first unread message

ghada glissa

unread,
Feb 2, 2015, 6:40:08 AM2/2/15
to
Dear all,

Is there any predefined function( not sizeof) in c++ that return the number of bytes on which is represented a decimal number.

exp:

56791 --> binary 1101110111010111 ---> represented on 2 bytes
56700911 --> binary 11011000010010111111101111 ----> represented on 4 bytes

Regards.
Message has been deleted

Scott Lurndal

unread,
Feb 2, 2015, 10:06:32 AM2/2/15
to
size_t bytes = (log2(56791) + 8)/8;


$ printf '%u\n' $(( (log2(56791) + 8)/8 ))
2
$ printf '%u\n' $(( (log2(56700911) + 8)/8 ))
4
$

scott

Vlad from Moscow

unread,
Feb 2, 2015, 11:54:30 AM2/2/15
to
What about negative numbers?

Christopher Pisz

unread,
Feb 2, 2015, 12:56:06 PM2/2/15
to
So, to clarify your question, are you asking what is the theoretical
minimum number of bytes that could be used to represent a given floating
point number?

If that's the case, no, there is no standard function for that, that I
know of.

However, a double is a double regardless of what it contains at the
time. If you are on Window's it will always be 8 bytes. Float - 4, and
so on. sizeof tells you this.




Geoff

unread,
Feb 2, 2015, 1:48:55 PM2/2/15
to
Short answer: No.

Define "decimal number". You have given examples of integers.

Do you mean any of the standard integral types or are you defining
your own? If you are defining your own then you must provide that
functionality if you need it.

In your examples the first one might be of type short, the second of
type int on a modern 32-bit compiler.

The size of any standard numeric type is invariant across that
particular type for that particular implementation. The sizeof
operator is the proper and standard method for obtaining the size of
those types.

One would use sizeof(long) or sizeof(int) sizeof(short) for example.

Geoff

unread,
Feb 2, 2015, 1:49:53 PM2/2/15
to
Where did he say anything about a floating point number?

Christopher Pisz

unread,
Feb 2, 2015, 2:04:06 PM2/2/15
to
I inferred it from his use of the word decimal. Same thing applies if it
is a whole number. Any type uses a number of bytes on a given
implementation that you would use sizeof to determine regardless of what
the variable contains.

He isn't being very clear in his question, thus the "are you asking?"
question.

I think he is looking for the minimum number of bytes it 'could' use,
but I am not sure.

ghada glissa

unread,
Feb 2, 2015, 2:52:52 PM2/2/15
to

>
> size_t bytes = (log2(56791) + 8)/8;
>
>
> $ printf '%u\n' $(( (log2(56791) + 8)/8 ))
> 2
> $ printf '%u\n' $(( (log2(56700911) + 8)/8 ))
> 4
> $
>
> scott

Thanks a lot for your valuable help.
0 new messages