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

G

Gregory Toomey

Manish_Ganvir said:
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
 
I

infobahn

Manish_Ganvir said:
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!
 
K

Keith Thompson

Manish_Ganvir said:
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.
 
C

Clark S. Cox III

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

Eric Sosman

Clark said:
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.
 
K

Keith Thompson

Eric Sosman said:
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.
 
M

Manish_Ganvir

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

Thanks
 
C

CBFalconer

Manish_Ganvir said:
It is a nice way to say that you dont know the solution, i really
appreciate your effort guys

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".
 
K

Keith Thompson

Manish_Ganvir said:
It is a nice way to say that you dont know the solution, i really
appreciate your effort guys

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
bottom of the article headers.

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.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top