Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
Manish_Ganvir
Guest
Posts: n/a
 
      02-12-2005
Is there any one line solution?

 
Reply With Quote
 
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!
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      02-12-2005
In article <95e0d849bcfa56da7920973f5dec1132
@localhost.talkaboutprogramming.com>, says...
> Is there any one line solution?


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

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-12-2005
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.

And the answer is perhaps.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-12-2005
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.

And the answer is perhaps.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== 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 =----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-12-2005
"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) kst- <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.
 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      02-13-2005
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


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-13-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: sizeof(const int *) > sizeof(int *) Stargazer C Programming 14 07-08-2010 02:01 PM
Re: sizeof(const int *) > sizeof(int *) Ben Bacarisse C Programming 12 07-02-2010 04:10 AM
An implementation where sizeof(short int) does not divide sizeof(int) Spiros Bousbouras C Programming 40 01-20-2007 09:58 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57