473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

how to know the size of an int

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
Nov 14 '05 #1
33 1480

"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
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
Nov 14 '05 #2
> 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
Nov 14 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jota wrote:
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);


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+UY3 sD1vfu2gCghUPj
8R74eXA3xWkV/Rk1x74fljk=
=jLRj
-----END PGP SIGNATURE-----
Nov 14 '05 #4
> 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...er/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
Nov 14 '05 #5
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)
//jota
Nov 14 '05 #6
> > 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

Nov 14 '05 #7
jota <ab*@hotmail.com> scribbled the following:
> 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) ;-)


What makes you think bytes are 8 bits in the first place?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane
Nov 14 '05 #8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jota wrote:
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)


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-----
Nov 14 '05 #9
> 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

- --
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-----

Nov 14 '05 #10
Lew Pitcher <Le*********@td.com> writes:
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.
--
"Am I missing something?"
--Dan Pop
Nov 14 '05 #11
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jota wrote:
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!


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+F3y j6FgwXHACg59Ij
pHOCsKF07AM6mfqH1+oryJc=
=jWER
-----END PGP SIGNATURE-----
Nov 14 '05 #12
Lew Pitcher <Le*********@td.com> writes:
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?
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #13
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ben Pfaff wrote:
Lew Pitcher <Le*********@td.com> writes:

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.


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+dtuy7jdSsQwdGB KTpE01BgCfXxoB
JTXuBs4JBw9UTkhu/9XuXVU=
=OX3H
-----END PGP SIGNATURE-----
Nov 14 '05 #14
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ben Pfaff wrote:
Lew Pitcher <Le*********@td.com> writes:

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?


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-----
Nov 14 '05 #15
On Mon, 05 Jul 2004 11:03:56 -0400, Lew Pitcher <Le*********@td.com>
wrote in comp.lang.c:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jota wrote:
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);


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.
}

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


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #16
On Mon, 05 Jul 2004 13:36:57 -0400, Lew Pitcher <Le*********@td.com>
wrote in comp.lang.c:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ben Pfaff wrote:
Lew Pitcher <Le*********@td.com> writes:

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.


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #17
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jack Klein wrote:
On Mon, 05 Jul 2004 11:03:56 -0400, Lew Pitcher <Le*********@td.com>
wrote in comp.lang.c:

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

jota wrote:

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);
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.


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-----
Nov 14 '05 #18
Lew Pitcher <Le*********@td.com> writes:
Ben Pfaff wrote:
Lew Pitcher <Le*********@td.com> writes:

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.


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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #19
Lew Pitcher <Le*********@td.com> writes:
Ben Pfaff wrote:
Lew Pitcher <Le*********@td.com> writes:

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?


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.
--
"The fact that there is a holy war doesn't mean that one of the sides
doesn't suck - usually both do..."
--Alexander Viro
Nov 14 '05 #20
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ben Pfaff wrote:
Lew Pitcher <Le*********@td.com> writes:

Ben Pfaff wrote:

Lew Pitcher <Le*********@td.com> writes:

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?


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)


Aha! The missing link!

Thanks. I seem to have missed reading that part of the standard. Next time my
examples won't have /that/ mistake.

- --
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)

iD8DBQFA6ahNagVFX4UWr64RArX2AJ9IBt+lx/0dMiKO3Jk2u4YqmBfFwQCdEGtc
nIs+ae3EounBVefTPaW+vcw=
=je44
-----END PGP SIGNATURE-----
Nov 14 '05 #21
On Mon, 05 Jul 2004 16:44:14 GMT, in comp.lang.c , "jota" <ab*@hotmail.com>
wrote:

Lew Pitcher wrote (and by the way, Jota its very annoying when people snip
all the attribution lines)
Bzzzt. Wrong answer. Thank you for playing.

Oh wow! Do U meen that ((sizeof(int)*8)+1) not equals to UINT_MAX on any
given platform?


Quite possibly. Where in the C standard does it state that a byte is 8 bits
wide?
But I sure as h--l does not agree to youre approximation theory!


You can think what you like, but anyone that assumes that a byte will
always be 8 bits is an idiot...
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #22
"jota" <ab*@hotmail.com> writes:
> 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) ;-)


^ does not mean what you think it means.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Nov 14 '05 #23
> > Witch ofcourse should have been ((2^(sizeof(int)*8))-1) ;-)

^ does not mean what you think it means.

Yeah it's just an expression! And I know it, and ofcourse so do you!
I figured that eventualy someone would point out that it's realy an XOR
story!
And since U realy knew what I ment in the first place,
please dont force me change my self once again!

Anyway did U think I ment ((0x02 ^ 0x20)-1) witch if I'm correct would be
((0x22)-1)
and I realy hope I'v got it right this time! ;-)

//jota
Nov 14 '05 #24
Evangelista Sami wrote:

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


Standard terminology for that, is "width",
rather "than size of, in bits"

--
pete
Nov 14 '05 #25
> You can think what you like, but anyone that assumes that a byte will
always be 8 bits is an idiot... And that also apply to everyone who will answer in an insulting way in
newsgroup
without reading ALL the postings!
Furthermore assuming that people are idiots might be something you might
keep to your self
I assume alot about people whos only accomplishment is comments like
Either you're a troll, or you're an idiot. Which is it? ... You can think what you like, but anyone that assumes that a byte will
always be 8 bits is an idiot... ... I have. Just because your own experience is limited, don't assume it equals the whole world.

And so forth... but I'll keep it to my self.
Anyway Mr Tyre! Your assumptions are off-topic.(noone here wants your
insults)
Now here is the place where I kindly lead you to the correct place for your
inner anger,
but I want do that!
//jota

Nov 14 '05 #26
"jota" <ab*@hotmail.com> writes:
> Witch ofcourse should have been ((2^(sizeof(int)*8))-1) ;-)


^ does not mean what you think it means.

Yeah it's just an expression! And I know it, and ofcourse so do you!


It's difficult to tell what you know, because you don't have much
of a history here. Maybe you know that ^ is "exclusive or", and
maybe you don't.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #27
In <if********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
On Mon, 05 Jul 2004 11:03:56 -0400, Lew Pitcher <Le*********@td.com>
wrote in comp.lang.c:
jota wrote:
>>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);
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"

^^^^^^
Can I have a chapter and verse for that?
conversion specifier. This produces undefined behavior.


Not if the type of sizeof(int) * CHAR_BIT is int and not size_t !

If you insist on anal correctness from the others, be anally correct
yourself!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #28
In <40***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Evangelista Sami wrote:

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


Standard terminology for that, is "width",
rather "than size of, in bits"


And the only way to figure the real width of an int is to examine the
value of INT_MAX. sizeof(int) * CHAR_BIT may also include padding bits.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #29
"Lew Pitcher" <Le*********@td.com> wrote in message
news:_3*******************@news20.bellglobal.com.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jota wrote:
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);


Bzzzt. Wrong answer. Thank you for playing.

printf("An int contains (at most) %d bits\n",sizeof(int) * CHAR_BIT);


Bzzzt! :-)
printf("An int contains (at most) %lu bits\n",

(unsigned long)sizeof(int) * CHAR_BIT);
-Mike

Nov 14 '05 #30
On Mon, 05 Jul 2004 16:51:42 GMT, "jota" <ab*@hotmail.com> wrote:
> 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


But still wrong, of course.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #31

"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
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


I always like empiricism:

unsigned int temp = ~0;
unsigned int bitcount = 1;

while (temp>>=1)++bitcount;

Or how about:

unsigned int bitcount = 0;
while (1 << (1+++bitcount)); // I wonder if this'll parse...

use these at your own risk.
Nov 14 '05 #32
Rufus V. Smith wrote:

"Evangelista Sami" <ev******@cnam.fr> wrote in message
news:5f**************************@posting.google.c om...
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


I always like empiricism:

unsigned int temp = ~0;


That should be:
unsigned int temp = ~0u;

The value of ~0, depends on the representation of negative integers.
In two's complement, ~0 is -1.
In one's complement, ~0 is -0.
In signed magnitude, ~0 is INT_MIN.

The rule for converting negative integer values to unsigned, is to
keep adding (UINT_MAX + 1) until a non negative value is reached.

-1u is UINT_MAX on all systems.

--
pete
Nov 14 '05 #33
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:xT*****************@newsread2.news.pas.earthl ink.net...
"Lew Pitcher" <Le*********@td.com> wrote in message
news:_3*******************@news20.bellglobal.com.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jota wrote:
>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);


Bzzzt. Wrong answer. Thank you for playing.

printf("An int contains (at most) %d bits\n",sizeof(int) * CHAR_BIT);


Bzzzt! :-)

printf("An int contains (at most) %lu bits\n",
(unsigned long)sizeof(int) * CHAR_BIT);


It's pretty safe to just use...

printf("An int contains (at most) %u bits\n",
(unsigned) sizeof(int) * CHAR_BIT);

....in this instance.

--
Peter
Nov 14 '05 #34

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

Similar topics

0
by: Laphan | last post by:
I know this is a crosspost, but I need to know which is the best one for posting INET FTP queries, so that I'm not cross-posting in future. Now that I've posted could somebody let me know: 1)...
0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
12
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
3
by: fadics | last post by:
I wrote a program with recursion to sort some data. When the number of data is too large, there will be a error, stack overflow. How can I do? And how can I know the size of system stack? ...
1
by: wukexin | last post by:
I write my own class Cfile, I want to know what about implement ctime().Who help me? My use function ctime, I sign it with $$$. my class Cfile: #------------------------ file.h...
6
by: Dima | last post by:
How can I know size of avaible memory in heap? For example : .... .... // size = N cout << "Size of Heap = " << SizeOfHeap() << endl; int* i = new int; // size = N - sizeof(int) cout << "Size...
9
by: Jim H | last post by:
In one of my functions I create a char string s, of dynamic allocated length. I want to free the memory before my function returns. Everywhere I find says free(s) is the way to do this, but I'm...
8
by: Phill | last post by:
Anyone know the internal working of this thing. I expected it to be kinda slow at enumerating large lists of objects but it was actually pretty good. I was able to write a quick linked list class...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
4
by: .rhavin grobert | last post by:
guess you have a bouquet of paddingless structs (and your compiler already cares for that) that all have one in common: their first memeber has to be their size. As fas as i know (am i right?) a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.