473,513 Members | 3,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

number of bits for an int type

Hello,
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?

thanks

Oct 16 '06 #1
14 3622
evang...@cnam.fr wrote:
Hello,
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?

thanks
Yes. However note that except for the type unsigned char, not all the
constituent bits of other types may be value bits, (i.e. bits which
hold the numeric value stored in the variable). There may be one or
more padding and trap bits.

So the range of values representable by the type need not be related to
the value returned by sizeof(T) * CHAR_BIT where T is the type. For
knowing the range, use the macros in <limits.hand <float.h>.

Oct 16 '06 #2
santosh a écrit :
evang...@cnam.fr wrote:
Hello,
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?

thanks

Yes. However note that except for the type unsigned char, not all the
constituent bits of other types may be value bits, (i.e. bits which
hold the numeric value stored in the variable). There may be one or
more padding and trap bits.

So the range of values representable by the type need not be related to
the value returned by sizeof(T) * CHAR_BIT where T is the type. For
knowing the range, use the macros in <limits.hand <float.h>.
thanks for your help,
in my program I manipulate many objects that I store in a dictionnary.
to save memory I encode these objects in arrays of ints before
inserting them in the dictionnary.
so is it right to assume that in each element of my array I have
"sizeof(int) * CHAR_BIT" bits?

Oct 16 '06 #3
ev******@cnam.fr wrote:
santosh a écrit :
evang...@cnam.fr wrote:
Hello,
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?
>
thanks
Yes. However note that except for the type unsigned char, not all the
constituent bits of other types may be value bits, (i.e. bits which
hold the numeric value stored in the variable). There may be one or
more padding and trap bits.

So the range of values representable by the type need not be related to
the value returned by sizeof(T) * CHAR_BIT where T is the type. For
knowing the range, use the macros in <limits.hand <float.h>.

thanks for your help,
in my program I manipulate many objects that I store in a dictionnary.
to save memory I encode these objects in arrays of ints before
inserting them in the dictionnary.
so is it right to assume that in each element of my array I have
"sizeof(int) * CHAR_BIT" bits?
Yes.

You might also want to read through:
http://c-faq.com/

Oct 16 '06 #4
posted:
Hello,
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?

Do a google search for IMAX_BITS.

IMAX_BITS is a macro which will tell you how many value representation bits
you have for a given integer value. For instance, if we had a 40-Bit int, 8
bits of which were padding bits, and 32 bits of which were value bits, then
the following would evaluate to the following:

sizeof(unsigned) == 40 / CHAR_BIT

IMAX_BITS( (unsigned)-1 ) == 32

--

Frederick Gotham
Oct 16 '06 #5
ev******@cnam.fr wrote:
>
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?
No, because there may be trap bits involved. However the number
range designated by INT_MIN and INT_MAX will give you the answer.
For unsigned int all you need is UINT_MAX. These are all defined
in <limits.h>.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Oct 16 '06 #6
CBFalconer <cb********@yahoo.comwrites:
ev******@cnam.fr wrote:
>>
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?

No, because there may be trap bits involved. However the number
range designated by INT_MIN and INT_MAX will give you the answer.
For unsigned int all you need is UINT_MAX. These are all defined
in <limits.h>.
Correction: padding bits, not trap bits. (Padding bits may or may not
contribute to trap *representations*.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 16 '06 #7
Frederick Gotham said:
posted:
>Hello,
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?


Do a google search for IMAX_BITS.

IMAX_BITS is a macro which will tell you how many value representation
bits you have for a given integer value.
Chapter and verse, please. I can find no reference to this macro in the ISO
C Standard (either C90 or C99).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 16 '06 #8
Richard Heathfield <in*****@invalid.invalidwrites:
Frederick Gotham said:
> posted:
>>Hello,
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?


Do a google search for IMAX_BITS.

IMAX_BITS is a macro which will tell you how many value representation
bits you have for a given integer value.

Chapter and verse, please. I can find no reference to this macro in the ISO
C Standard (either C90 or C99).
Because its a home made macro discussed previously. Its in google.

Takes about 0.001 of a second for google to locate it.

Clever too : considering its calculating the number of bits for an int
*VALUE* and not the int type ......
Oct 16 '06 #9
Richard Heathfield <in*****@invalid.invalidwrites:
Frederick Gotham said:
> posted:
>>Hello,
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?


Do a google search for IMAX_BITS.

IMAX_BITS is a macro which will tell you how many value representation
bits you have for a given integer value.

Chapter and verse, please. I can find no reference to this macro in the ISO
C Standard (either C90 or C99).
http://groups.google.com/group/comp....489b673063404c

define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))

(I don't think there was any claim that it's defined in the standard.)

See the rest of the thread in comp.std.c for some discussion. (I
haven't tried using it myself.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 16 '06 #10
Keith Thompson <ks***@mib.orgwrites:
>define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))
And add 1 for the sign bit if the argument is for a signed type.

--
Hallvard
Oct 18 '06 #11
In article <hb*************@bombur.uio.no>,
Hallvard B Furuseth <h.**********@usit.uio.nowrote:
>Keith Thompson <ks***@mib.orgwrites:
>>define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))
>And add 1 for the sign bit if the argument is for a signed type.
If the argument is a signed type, then the sign of (-1) % %0x3fffffffL
is not nailed down (at least not by C89). It could just give you
-1 back as the result. Add 1 to that in the ((m)%0x3fffffffL+1)
expression and you get 0, so you would be dividing m by 0... at
which point you are lost.

If you adjust the mod expressions by changing expressions of the
form t % n into (t + n) % n, then you get around the division by 0,
but you then immediately run into the problem that -1 divided by
anything larger than 1 is going to be 0. Unless your representation
of -1 is exceedingly small, IMAX_BITS() feed a signed 1 is
always going to return the result 79.
>And add 1 for the sign bit if the argument is for a signed type.
That presumes that there is a seperate sign bit, and exactly one of
them. C89 does not promise either; C99 is a bit tighter on this,
but not enough so that a constant +1 would work.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Oct 18 '06 #12
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>ev******@cnam.fr wrote:
>>How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?
No, because there may be trap bits involved. However the number
range designated by INT_MIN and INT_MAX will give you the answer.
For unsigned int all you need is UINT_MAX. These are all defined
in <limits.h>.

Correction: padding bits, not trap bits. (Padding bits may or may not
contribute to trap *representations*.)
Can we simply count the bits?

#include <stdio.h>
int main(void) {
unsigned ui = -1;
int i = 0;
while (ui) {
ui <<= 1;
++i;
}
printf("%d\n", i);
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Oct 18 '06 #13
Joe Wright <jo********@comcast.netwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
>>ev******@cnam.fr wrote:
How can I know the number of bits used for an int value?
Is "sizeof(int) * CHAR_BIT" correct ?
No, because there may be trap bits involved. However the number
range designated by INT_MIN and INT_MAX will give you the
answer. For unsigned int all you need is UINT_MAX. These are all
defined
in <limits.h>.
Correction: padding bits, not trap bits. (Padding bits may or may
not
contribute to trap *representations*.)
Can we simply count the bits?

#include <stdio.h>
int main(void) {
unsigned ui = -1;
int i = 0;
while (ui) {
ui <<= 1;
++i;
}
printf("%d\n", i);
return 0;
}
Sure, but that's O(N) (where N is the number of value bits), and it
doesn't give you a constant expression. If neither of those is a
problem, then counting the bits is certainly a good approach.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 18 '06 #14
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>Hallvard B Furuseth <h.**********@usit.uio.nowrote:
>>Keith Thompson <ks***@mib.orgwrites:
>>>
define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))
>>And add 1 for the sign bit if the argument is for a signed type.

If the argument is a signed type, then the sign of (-1) % %0x3fffffffL
is not nailed down (at least not by C89).
Hm, that thread seems to be missing the documentation. It returns #bits
in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 3.2E+10. E.g.
IMAX_BITS(INT_MAX) + 1 for #bits in the value INT_MAX, + 1 for sign bit.
Except...
>>And add 1 for the sign bit if the argument is for a signed type.

That presumes that there is a seperate sign bit, and exactly one of
them. C89 does not promise either;
Hmm. I think it does promise separate sign bit(s), since "The range of
nonnegative values of a signed integer type is a subrange of the
corresponding unsigned integer type, and the representation of the same
value in each type is the same." (ANSI draft, 3.1.2.5p5.)

I never thought of multiple sign bits though. Yuck. Does that mean
that we can have (a == INT_MIN && b == INT_MIN && (a & b) == 0)? a and
b could have different sign bits set - two's complement except that
there are several sign bit and a value is negative if at least one sign
bit is negative. Anyway, I can't say I'll worry about such a beast
until I hear about one:-)
C99 is a bit tighter on this, but not enough so that a constant +1
would work.
Huh? C99 6.2.6.2p2: For signed integer types (...) there shall be
exactly one sign bit.
BTW, I posted a bunch of macros for log2() of any positive constant
integer some time ago too, but that was several lines of macros calling
each others, yielding one _huge_ expression.
http://groups.google.com/group/comp....6324f25e4a60b0

--
Hallvard
Oct 19 '06 #15

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

Similar topics

21
4495
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
0
1858
by: chris | last post by:
What is the recommended way of storing 10 digit phone numbers? I like bigint(10), but it seems most are using varchar? Or I've also seen 2 int columns, one for area code and another for the rest of the digits? I wish phone numbers had their own column type, so I can send 123-456-7890 or (123) 456-7890 or whatever and have mysql do the...
5
1423
by: Heiko Wundram | last post by:
Hi all! I'm wondering whether there is some form of number set type implemented in pure Python. I'm currently in the process of implementing one myself (for an IPv4 address range type), and if there's some form of reference implementation I'd love to see it. I've not found a reasonably complete implementation anywhere so far. To explain...
5
1873
by: dananrg | last post by:
I was messing around with the native ODBC module (I am using Python in a Win32 environment), e.g: import dbi, odbc ....and it seems to meet my needs. I'd rather use a module that comes natively with Python if it works (don't care about performance in this particular use case; just that it works). The only issue I've had so far is...
4
2049
by: Alan Ning | last post by:
Hi all, Say I have the following, string str("Hello World\n") I would like to extract 5 bits at a time from the string. I am doing this to convert ascii string to another string type. Is there a way to do this with stringstream and bitset?
11
14637
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
77
4206
by: borophyll | last post by:
As I read it, C99 states that a byte is an: "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" (3.6) and that a byte must be at least 8 bits: "The values given below shall be replaced by constant expressions suitable for use in #if
4
5771
by: kcddoorman | last post by:
I'm trying to make a multi criteria server filter and everything works fine when filtering for strings. When I add a number field to the filterable set I run into problems. Here is the VBScript I'm using. <SCRIPT language=vbscript> Function SetServerFilter() dim myFilter, strCustomer, strJobName, strSalesPerson, strWorkOrder If...
5
7078
by: Amit Parmar | last post by:
Hi Everybody, I am fetching number data from a table. i want to add '$' sign to this data. I already tried "TO_CHAR" function. This works fine but it converts number data type to string data type. I want it to be in number format only. So, is there any way or any function that add '$' sign to number data without converting its data type? ...
2
9493
by: JoeKid09 | last post by:
Hi Guys, I'm working on converting a large table with a field that has numeric values in text data type. When I do the conversion in Access automatically some records don't get converted. The reason why is because the field that holds values in the table has the following data ( 9.22337E+18 ) now this happened because the data was imported...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
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
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
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...
0
7543
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
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
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.