Velocity Reviews > How to calculate size of an int without using the sizeof operator but using bitwise operator

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

Manish_Ganvir
Guest
Posts: n/a

 02-12-2005
Please do not use pointer arithmetic or for loops
Solution

Gregory Toomey
Guest
Posts: n/a

 02-12-2005
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

Manish_Ganvir
Guest
Posts: n/a

 02-12-2005
Is there any one line solution?

infobahn
Guest
Posts: n/a

 02-12-2005
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!

Randy Howard
Guest
Posts: n/a

 02-12-2005
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

Mark McIntyre
Guest
Posts: n/a

 02-12-2005
On Sat, 12 Feb 2005 04:31:19 -0500, in comp.lang.c , "Manish_Ganvir"
<(E-Mail Removed)> 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
Guest
Posts: n/a

 02-12-2005
On Sat, 12 Feb 2005 04:31:19 -0500, in comp.lang.c , "Manish_Ganvir"
<(E-Mail Removed)> 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 =----

Keith Thompson
Guest
Posts: n/a

 02-12-2005
"Manish_Ganvir" <(E-Mail Removed)> 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) (E-Mail Removed) <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.

Clark S. Cox III
Guest
Posts: n/a

 02-13-2005
On 2005-02-12 04:25:55 -0500, Gregory Toomey <(E-Mail Removed)> 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
(E-Mail Removed)

Eric Sosman
Guest
Posts: n/a

 02-13-2005
Clark S. Cox III wrote:

> On 2005-02-12 04:25:55 -0500, Gregory Toomey <(E-Mail Removed)> 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
(E-Mail Removed)lid