Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to calculate size of an int without using the sizeof operator but using bitwise operator (http://www.velocityreviews.com/forums/t436892-how-to-calculate-size-of-an-int-without-using-the-sizeof-operator-but-using-bitwise-operator.html)

 Manish_Ganvir 02-12-2005 09:21 AM

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

Please do not use pointer arithmetic or for loops
Solution

 Gregory Toomey 02-12-2005 09:25 AM

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

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 02-12-2005 09:31 AM

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

Is there any one line solution?

 infobahn 02-12-2005 09:42 AM

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!

 Randy Howard 02-12-2005 10:47 AM

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

 Mark McIntyre 02-12-2005 07:09 PM

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"
<manishnew@rediffmail.com> 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 02-12-2005 07:30 PM

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"
<manishnew@rediffmail.com> 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 02-12-2005 10:14 PM

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

"Manish_Ganvir" <manishnew@rediffmail.com> 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) kst-u@mib.org <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 02-13-2005 03:39 AM

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

On 2005-02-12 04:25:55 -0500, Gregory Toomey <nospam@bigpond.com> 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
clarkcox3@gmail.com

 Eric Sosman 02-13-2005 03:55 PM

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 <nospam@bigpond.com> 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
esosman@acm-dot-org.invalid

All times are GMT. The time now is 03:55 PM.