# How to calculate size of an int without using the sizeof operator but using bitwise operator

Discussion in 'C Programming' started by Manish_Ganvir, Feb 12, 2005.

1. ### Manish_GanvirGuest

Please do not use pointer arithmetic or for loops
Solution

Manish_Ganvir, Feb 12, 2005

2. ### Gregory ToomeyGuest

Manish_Ganvir wrote:

> Please do not use pointer arithmetic or for loops
> Solution

Set an unsigned int to 1, use the left shift operator << , test whether its
zero.

gtoomey

Gregory Toomey, Feb 12, 2005

3. ### Manish_GanvirGuest

Re: How to calculate size of an int without using the sizeof ope

Is there any one line solution?

Manish_Ganvir, Feb 12, 2005
4. ### infobahnGuest

Re: How to calculate size of an int without using the sizeof operatorbut using bitwise operator

Manish_Ganvir wrote:
>
> Please do not use pointer arithmetic or for loops
> Solution

#include <stdio.h>

#define FAIRLY_LARGE 32767

int main(void)
{
char buf[FAIRLY_LARGE] = {0};
puts("Please enter the size of an int, in bytes.");
if(fgets(buf, FAIRLY_LARGE, stdin) != NULL)
{
~fputs("The size of an int, in bytes, is ", stdout);
puts(buf);
}
return 0;
}

Now here's a little puzzle for you in return:

RG8gWW91ciBPd24gSG9tZXdvcmshDQo=

Enjoy!

infobahn, Feb 12, 2005
5. ### Randy HowardGuest

Re: How to calculate size of an int without using the sizeof ope

In article <95e0d849bcfa56da7920973f5dec1132
> Is there any one line solution?

The oracle declares that you owe 50 pounds of chocolate chips for
help with incredibly stupid homework assignments.

--
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig

Randy Howard, Feb 12, 2005
6. ### Mark McIntyreGuest

Re: How to calculate size of an int without using the sizeof ope

On Sat, 12 Feb 2005 04:31:19 -0500, in comp.lang.c , "Manish_Ganvir"
<> wrote:

>Is there any one line solution?

To what? Its a good idea to put your actual question in the message body.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

Mark McIntyre, Feb 12, 2005
7. ### Mark McIntyreGuest

Re: How to calculate size of an int without using the sizeof ope

On Sat, 12 Feb 2005 04:31:19 -0500, in comp.lang.c , "Manish_Ganvir"
<> wrote:

>Is there any one line solution?

To what? Its a good idea to put your actual question in the message body.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre, Feb 12, 2005
8. ### Keith ThompsonGuest

Re: How to calculate size of an int without using the sizeofoperator but using bitwise operator

"Manish_Ganvir" <> writes:
> Please do not use pointer arithmetic or for loops
> Solution

Q: How do I compute the size of an int without using sizeof?
A: Use sizeof. That's what it's for.

Q: But how do I compute the size of an int *without using sizeof*?
A: Why would you want to do a silly thing like that?

Q: It's a homework assignment.
A: Then give us your instructor's e-mail address so we can submit the
solution directly. You wouldn't want to deny us credit for our
work, would you?

Q: But then how do I get class credit?
A: I guess you don't.

--
Keith Thompson (The_Other_Keith) <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.

Keith Thompson, Feb 12, 2005
9. ### Clark S. Cox IIIGuest

On 2005-02-12 04:25:55 -0500, Gregory Toomey <> said:

> Manish_Ganvir wrote:
>
>> Please do not use pointer arithmetic or for loops
>> Solution

>
> Set an unsigned int to 1, use the left shift operator << , test whether its
> zero.

That won't always work (think padding-bits, trap representations, etc.).

--
Clark S. Cox, III

Clark S. Cox III, Feb 13, 2005
10. ### Eric SosmanGuest

Re: How to calculate size of an int without using the sizeof operatorbut using bitwise operator

Clark S. Cox III wrote:

> On 2005-02-12 04:25:55 -0500, Gregory Toomey <> said:
>
>> Manish_Ganvir wrote:
>>
>>> Please do not use pointer arithmetic or for loops
>>> Solution

>>
>>
>> Set an unsigned int to 1, use the left shift operator << , test
>> whether its
>> zero.

>
>
> That won't always work (think padding-bits, trap representations, etc.).

Not applicable. The shift operator works with
the values of its operands, not with their representation.
Left-shifting a 1 by 1 bit always produces 2, even if
forty-two padding bits lie between the units' and twos'
positions. Furthermore, shifting a valid unsigned
value (e.g., 1) always produces a valid unsigned value
and never produces a trap representation.

--
Eric Sosman
lid

Eric Sosman, Feb 13, 2005
11. ### Keith ThompsonGuest

Re: How to calculate size of an int without using the sizeofoperator but using bitwise operator

Eric Sosman <> writes:
> Clark S. Cox III wrote:
>
>> On 2005-02-12 04:25:55 -0500, Gregory Toomey <> said:
>>
>>> Manish_Ganvir wrote:
>>>
>>>> Please do not use pointer arithmetic or for loops
>>>> Solution
>>>
>>>
>>> Set an unsigned int to 1, use the left shift operator << , test
>>> whether its
>>> zero.

>>
>>
>> That won't always work (think padding-bits, trap representations, etc.).

>
> Not applicable. The shift operator works with
> the values of its operands, not with their representation.

Right, and the question was about the representation, specifically the
size.

If you have a 32-bit integer type with 8 padding bits, any solution
using bitwise operations will tell you it has 24 bits. The question
was about its size, 32 bits, not its width.

--
Keith Thompson (The_Other_Keith) <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.

Keith Thompson, Feb 13, 2005
12. ### Manish_GanvirGuest

Re: How to calculate size of an int without using the sizeof

It is a nice way to say that you dont know the solution, i really

Thanks

Manish_Ganvir, Feb 14, 2005
13. ### CBFalconerGuest

Re: How to calculate size of an int without using the sizeof

Manish_Ganvir wrote:
>
> It is a nice way to say that you dont know the solution, i really

No quotes again. What does 'it' refer to? Solution to what? I
suspect that 'it' is really a toned down version of "stop wasting
our time".

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

CBFalconer, Feb 14, 2005
14. ### Keith ThompsonGuest

Re: How to calculate size of an int without using the sizeof

"Manish_Ganvir" <> writes:
> It is a nice way to say that you dont know the solution, i really

Following the References header indicates that this was a followup to
my article, but you didn't provide any context.

If you want to post a followup via groups.google.com, don't use the
broken "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at the

Most of us either know a solution, or could figure one out if we spent
the time on it, or (depending on how your question is interpreted)
could demonstrate that no solution is possible. We simply aren't
interested in doing your homework for you.

If you have some valid reason for this rather odd question other than
a homework assignment, I'm sure many of us would be glad to help.

--
Keith Thompson (The_Other_Keith) <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.

Keith Thompson, Feb 14, 2005