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. Please do not use pointer arithmetic or for loops
    Solution
     
    Manish_Ganvir, Feb 12, 2005
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

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

    Is there any one line solution?
     
    Manish_Ganvir, Feb 12, 2005
    #3
  4. Manish_Ganvir

    infobahn Guest

    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
    #4
  5. Manish_Ganvir

    Randy Howard Guest

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

    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
     
    Randy Howard, Feb 12, 2005
    #5
  6. 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.

    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>
     
    Mark McIntyre, Feb 12, 2005
    #6
  7. 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.

    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 =----
     
    Mark McIntyre, Feb 12, 2005
    #7
  8. 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
    #8
  9. 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
    #9
  10. Manish_Ganvir

    Eric Sosman Guest

    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
    #10
  11. 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
    #11
  12. 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
    appreciate your effort guys

    Thanks
     
    Manish_Ganvir, Feb 14, 2005
    #12
  13. Manish_Ganvir

    CBFalconer Guest

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

    --
    "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." - Keith Thompson
     
    CBFalconer, Feb 14, 2005
    #13
  14. 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
    > 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.

    --
    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
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,416
    Ron Natalie
    Oct 14, 2004
  2. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    579
    Joe Smith
    May 22, 2006
  3. Spiros Bousbouras
    Replies:
    40
    Views:
    1,541
    Keith Thompson
    Jan 20, 2007
  4. Stargazer

    Re: sizeof(const int *) > sizeof(int *)

    Stargazer, Jun 14, 2010, in forum: C Programming
    Replies:
    14
    Views:
    690
    Ben Bacarisse
    Jul 8, 2010
  5. Ben Bacarisse

    Re: sizeof(const int *) > sizeof(int *)

    Ben Bacarisse, Jun 14, 2010, in forum: C Programming
    Replies:
    12
    Views:
    456
    David Thompson
    Jul 2, 2010
Loading...

Share This Page