how to know the size of an int

  • Thread starter Evangelista Sami
  • Start date
E

Evangelista Sami

hello

how can i know the size of an int in bits?
i looked in limits.h but i did not find anything to get this

thanks for any help
 
M

Mike Wahler

Evangelista Sami said:
hello

how can i know the size of an int in bits?
i looked in limits.h but i did not find anything to get this

#include <limits.h>
#include <stdio.h>

int main()
{
printf("Type 'int' occupies %lu bits on this implementation\n",
(unsigned long)sizeof(int) * CHAR_BIT);

return 0;
}

-Mike
 
J

jota

how can i know the size of an int in bits?
i looked in limits.h but i did not find anything to get this


printf("sizeof(int)=%d bits",sizeof(int)*8);
//jota
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
printf("sizeof(int)=%d bits",sizeof(int)*8);

Bzzzt. Wrong answer. Thank you for playing.

At best, you're going to get an /approximation/ of the number of bits in an int
with...

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void)
{
printf("An int contains (at most) %d bits\n",sizeof(int) * CHAR_BIT);
return EXIT_SUCCESS;
}

With inspection and proper evaluation of UINT_MAX and INT_MAX, you can deduce
the minimum number of bits allocated to an int.


- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFA6W3aagVFX4UWr64RArKxAKDvBxQ6l78Z1kps6j+UY3sD1vfu2gCghUPj
8R74eXA3xWkV/Rk1x74fljk=
=jLRj
-----END PGP SIGNATURE-----
 
J

jota

Bzzzt. Wrong answer. Thank you for playing.
At best, you're going to get an /approximation/ of the number of bits in an int
with...

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void)
{
printf("An int contains (at most) %d bits\n",sizeof(int) * CHAR_BIT);
return EXIT_SUCCESS;
}

With inspection and proper evaluation of UINT_MAX and INT_MAX, you can deduce
the minimum number of bits allocated to an int.

Oh wow! Do U meen that ((sizeof(int)*8)+1) not equals to UINT_MAX on any
given platform?
Ofcourse I read about your skills at
http://www.salmar.com/pipermail/wftl-lug/2001-November/000249.html
but I think U have to agree that it is almoust certenly true!
Ofcourse U R right, I agree to 'not always true'!
But I sure as h--l does not agree to youre approximation theory!
Note should be given that I should have writen (sizeof(int)*CHAR_BIT) in the
first place!
//jota
 
J

jota

Oh wow! Do U meen that ((sizeof(int)*8)+1) not equals to UINT_MAX on any
Should have been ((2^(sizeof(int)*8))+1)
Witch ofcourse should have been ((2^(sizeof(int)*8))-1) ;-)
//jota
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Should have been ((2^(sizeof(int)*8))+1)

Yes, I mean that
(1 << (sizeof(int) * 8)) - 1
is not necessarily equal to UINT_MAX

First off, what leads you to believe that C allocates storage exclusively in
8-bit quantities?

Secondly, what leads you to believe that /all/ of the storage allocated to a
particular data type is used by that data type?

It is entirely possible to have (sizeof(int)*CHAR_BIT) == 64, but UINT_MAX only
65537. (I didn't say that it is /common/, I just said that it is /possible/)

- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFA6Yk+agVFX4UWr64RAkK6AJ9rOv/pUXOZ9vINbGE/CmwMel2wTwCgwiMJ
b3XN2LsM1gBE38LGGkM0xdA=
=frOO
-----END PGP SIGNATURE-----
 
B

Ben Pfaff

Lew Pitcher said:
Yes, I mean that
(1 << (sizeof(int) * 8)) - 1
is not necessarily equal to UINT_MAX

When CHAR_BIT == 8, that expression is undefined, because
sizeof(int) * 8 will be equal to (or greater than) the width of
an int.
 
J

jota

It is entirely possible to have (sizeof(int)*CHAR_BIT) == 64, but UINT_MAX
only
65537. (I didn't say that it is /common/, I just said that it is
/possible/)
Yes! I know my PIC18 compiler UINT_MAX const is
#define UINT_MAX 65535 /* unsigned int */
...but i certenly expect sizeof(int) to be 2 in that case
...ofcourse CHAR_BIT is 8 under that env. but I can certenly se senarios
where it might not be!
//jota
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
/possible/)
Yes! I know my PIC18 compiler UINT_MAX const is
#define UINT_MAX 65535 /* unsigned int */
..but i certenly expect sizeof(int) to be 2 in that case
..ofcourse CHAR_BIT is 8 under that env. but I can certenly se senarios
where it might not be!

Stick around here, and you soon will see scenarios where CHAR_BIT is some value
other than 8, and UINT_MAX is less than (1 << (sizeof(int) * CHAR_BIT) - 1)

You can also check out the comp.*.embedded newsgroups, where such is common,
especially on things like 64bit DSPs

- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFA6ZDkagVFX4UWr64RAkFkAKCqlDny6NHak+P+WQ+F3yj6FgwXHACg59Ij
pHOCsKF07AM6mfqH1+oryJc=
=jWER
-----END PGP SIGNATURE-----
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ben said:
When CHAR_BIT == 8, that expression is undefined, because
sizeof(int) * 8 will be equal to (or greater than) the width of
an int.

Ben, could you explain how the expression is undefined if
CHAR_BIT == 8
and
sizeof(int) == 3
and
UINT_MAX == 66000
?

I think I see your point, but I'd like to be sure.

- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFA6ZG2agVFX4UWr64RAoPRAKCdqlB+dtuy7jdSsQwdGBKTpE01BgCfXxoB
JTXuBs4JBw9UTkhu/9XuXVU=
=OX3H
-----END PGP SIGNATURE-----
 
B

Ben Pfaff

Lew Pitcher said:
It is entirely possible to have (sizeof(int)*CHAR_BIT) == 64, but UINT_MAX only
65537. (I didn't say that it is /common/, I just said that it is /possible/)

I don't think a UINT_MAX of 65537 is possible, is that just a
typo for 65535?
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ben said:
I don't think a UINT_MAX of 65537 is possible, is that just a
typo for 65535?

No, it was intended.

I didn't have a real implementation in mind, just something unusual that still
conforms to the standards. AFAIK, the standards don't specify that UINT_MAX must
be some power-of-two minus 1; they simply say that the minimum value for
UINT_MAX is 65535. So, I chose a value for UINT_MAX that is greater than 65535,
but less than 131072.

The scenario I was trying to invent was
a) a conforming implementation that
b) used a UINT_MAX > 65535
c) used a CHAR_BIT > 8
d) used padding bits in the storage of an int
such that the number of bits used to store the
significant value of the int is less than the number of bits
allocated in the int storage class.

Of course, I could have said that
"It is entirely possible to have (sizeof(int)*CHAR_BIT) == 24, but UINT_MAX
only 131071."
but that wouldn't been unusual enough a value.


- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFA6Zm9agVFX4UWr64RAgM/AKCt08HjV4bjGQkOI+PZw2uG/08FnQCfaI8E
sAaPz35go83+zfzdV1wLZd4=
=e7LU
-----END PGP SIGNATURE-----
 
J

Jack Klein

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Bzzzt. Wrong answer. Thank you for playing.

At best, you're going to get an /approximation/ of the number of bits in an int
with...

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void)
{
printf("An int contains (at most) %d bits\n",sizeof(int) * CHAR_BIT);
return EXIT_SUCCESS;

Bzzzt. Wrong answer. Thank you for playing.

Your code above passes a value of type size_t to printf() with a "%d"
conversion specifier. This produces undefined behavior.
 
J

Jack Klein

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



Ben, could you explain how the expression is undefined if
CHAR_BIT == 8
and
sizeof(int) == 3
and
UINT_MAX == 66000
?

I think I see your point, but I'd like to be sure.

Assume CHAR_BIT is 8, and sizeof(int) is 4, as on a typical desk-top
platform. Then your expression:

(1 << (sizeof(int) * 8)) - 1

....evaluates to this, after evaluation of "sizeof(int)":

(1 << 32) - 1

Since we postulated CHAR_BIT of 8 and sizeof(int) of 4, the maximum
number of bits in an int is 32, and you are left shifting the literal
constant 1 (which has type int) left by 32 bits.

Shifting an integer type by a negative value, or a value greater than
or equal to the number of its bits is undefined.

Take away the assumption that CHAR_BIT is 8, and specify that is in
fact greater than 8, then the expression is well-defined but has
nothing at all to do with the value of UINT_MAX.

If you used a long or long long constant (1L, 1UL, 1LL, or 1ULL), then
you would have been OK on implementations where these types have a
greater bit-width than int, which is some but not all.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jack said:
Bzzzt. Wrong answer. Thank you for playing.

I /knew/ that was incorrect, the moment I posted it.
Your code above passes a value of type size_t to printf() with a "%d"
conversion specifier. This produces undefined behavior.

So, corrected, it should be something like...

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void)
{
printf("An int contains (at most) %d bits\n",
(int)(sizeof(int) * CHAR_BIT));
return EXIT_SUCCESS;
}


- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFA6aPqagVFX4UWr64RArcqAJ9oRWZKrby6rI/geK/dNrdpw9X8YgCdEKG2
u9tM3Ujt04h3LZ7yBhLuKSQ=
=Fksz
-----END PGP SIGNATURE-----
 
B

Ben Pfaff

Lew Pitcher said:
Ben, could you explain how the expression is undefined if
CHAR_BIT == 8
and
sizeof(int) == 3
and
UINT_MAX == 66000
?

C99 6.5.7 says "If the value of the right operand is negative or
is greater than or equal to the width of the promoted left
operand, the behavior is undefined."

C99 6.2.6.2 defines width: "The precision of an integer type is
the number of bits it uses to represent values, excluding any
sign and padding bits. The width of an integer type is the same
but including any sign bit; thus for unsigned integer types the
two values are the same, while for signed integer types the width
is one greater than the precision."

Thus, in your example the width of `int' is, um, approximately
16.01 bits. (I don't think this is valid, but I'll cover that in
another email.) Your expression simplifies to (1 << 24) - 1.
Because 24 >= 16.01, the behavior is undefined.
 
B

Ben Pfaff

Lew Pitcher said:
No, it was intended.

I didn't have a real implementation in mind, just something unusual that still
conforms to the standards. AFAIK, the standards don't specify that UINT_MAX must
be some power-of-two minus 1; they simply say that the minimum value for
UINT_MAX is 65535. So, I chose a value for UINT_MAX that is greater than 65535,
but less than 131072.

See C99 6.2.6.2, for example:

1 For unsigned integer types other than unsigned char, the bits
of the object representation shall be divided into two
groups: value bits and padding bits (there need not be any
of the latter). If there are N value bits, each bit shall
represent a different power of 2 between 1 and 2**(N-1), so
that objects of that type shall be capable of representing
values from 0 to 2**N - 1 using a pure binary
representation; this shall be known as the value
representation. The values of any padding bits are
unspecified.44)

(I am representing the standard's superscripts as **.) I don't
see how you can set a maximum value of 65537 and still comply
with this paragraph.
 

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

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top