Re: make a program using C,C++



On Tue, 17 Jan 2006, pete <pfiland@xxxxxxxxxxxxxx> wrote:-

David Bolt wrote:

Even though it's looks like this is the solution
to a homework question,
is there any reason for not doing it this way:

    printf("\nBinary: ");
    for(counter=(sizeof(int)*8);counter>=0;--counter)
    {
        temp = 1 << (counter);
        printf("%d", (number & temp) ? 1 : 0);
    }

What is (1 << (sizeof(int)*8)) supposed to be?
^^^^^^^^^^^^^
gives the number of bits in an int and left-shifts 1 that number of times.


I suppose that I should have used:

for(counter=(sizeof(int)*CHAR_BIT);counter>=0;--counter)
^^^^^^^^^
However, I'm guessing that you missed the above, which would mean that mean you missed that for the first run through the loop:


temp = 1 << (counter);

would be the equivalent of:

temp = 1 << ((sizeof(int)*8)-1)


Regards, David Bolt

--
Member of Team Acorn checking nodes at 50 Mnodes/s: http://www.distributed.net/
AMD1800 1Gb WinXP/SUSE 9.3  | AMD2400 256Mb SuSE 9.0  | A3010 4Mb RISCOS 3.11
AMD2400(32) 768Mb SUSE 10.0 | RPC600 129Mb RISCOS 3.6 | Falcon 14Mb TOS 4.02
AMD2600(64) 512Mb SUSE 10.0 | A4000 4Mb RISCOS 3.11   | STE 4Mb TOS 1.62
.



Relevant Pages

  • Re: make a program using C,C++
    ... to a homework question, ... gives the number of bits in an int and left-shifts 1 that number of times. ... too big to be represented in an int thus causing an overflow ...
    (comp.lang.c)
  • Re: help with this game
    ... 'int' object is not callable ... You only want to give players 10 chances to guess the number - but, ... If the player keeps guessing ... > ok now that i know the random function, ...
    (comp.lang.python)
  • Re: Early C ?
    ... Most of the tutorial seems to make sense too me, what am I missing? ... (Not a homework question, just trying to improve my skills) ... you got 'int' and 'int' was the size of a machine word. ...
    (comp.arch.embedded)
  • Re: Early C ?
    ... (Not a homework question, just trying to improve my skills) ... you got 'int' and 'int' was the size of a machine word. ... Thanks, Pete ...
    (comp.arch.embedded)
  • Re: MySQL ResultSet - count rows?
    ... Lew wrote: ... Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ... int n = rs.getRow; ... I'm guessing that it's the overhead of going first / last / first on large datasets. ...
    (comp.lang.java.databases)