how to know the size of an int

  • Thread starter Evangelista Sami
  • Start date
L

Lew Pitcher

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

Ben said:
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-----
 
M

Mark McIntyre

Lew Pitcher wrote (and by the way, Jota its very annoying when people snip
all the attribution lines)
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...
 
J

jota

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
 
P

pete

Evangelista 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

thanks for any help

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

jota

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
 
B

Ben Pfaff

jota said:
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.
 
D

Dan Pop

In said:
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
 
D

Dan Pop

In said:
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
 
M

Mike Wahler

Lew Pitcher said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


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
 
R

Rufus V. Smith

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

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

pete

Rufus said:
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.
 
P

Peter Nilsson

Mike Wahler said:
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.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top