Thread: java to c.

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    java to c.

    hey everybody, ive just started studying the language, im familiar with java so people told me its relatively easy... well, i have couple of questions if someone is willing to help.
    in this example:
    for(n=0,.......)
    n=10*n+(s[i]-'0');
    why is it written s[i]-'0'? what is the meaning behind it?
    and another question is there any way to transfer an int array in base 10 to base 2? and if there is what is the process?
    tnx for any help.

  2. #2
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    why is it written s[i]-'0'? what is the meaning behind it?
    s is an array, and s[i] gives us the the value of the element at index i in array.
    '0' is a character representation of the number zero.
    This character is represented as an ascii code(which is really just a number) which is why we can subtract the character '0'.
    The ascii-code of '0' is the decimal number 48, so they might just as well have written:
    s[i] - 48.
    Last edited by Drogin; 04-11-2009 at 08:49 AM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by radxxx View Post
    hey everybody, ive just started studying the language, im familiar with java so people told me its relatively easy... well, i have couple of questions if someone is willing to help.
    in this example:
    for(n=0,.......)
    n=10*n+(s[i]-'0');
    why is it written s[i]-'0'? what is the meaning behind it?
    and another question is there any way to transfer an int array in base 10 to base 2? and if there is what is the process?
    tnx for any help.
    s[i] - '0' is the standard idiom for converting a char in the 0 to 9 range, to a digit of a number. The char '9' becomes the number 9 by subtracting '0' from it.

    10n + s[i] - '0' could be the conversion from a 2D array, (10 X ??) to a 1D array. Same as 10*row + column.

    One way is to take the highest power of 2 that is less than your base 10 number, and divide into it. Store this intermediate result. Repeat that, storing all the intermediate results, until you have either a 1 or 0 left over. Now add all the intermediate results together, using base 2 rules:

    So if the base 10 number was 11:

    11 == it's 2^3 (in a loop, 2 * 2 * 2, get it?) or 100, with 3 remainder

    100 == 2^3rd
    +011 (3 remainder) (you can repeat this same process with the remainder, to get the 011.
    ====
    111 in base 2 == 11 in base 10.
    Last edited by Adak; 04-11-2009 at 08:46 AM.

  4. #4
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    So if the base 10 number was 11:

    11 == it's 2^3 (in a loop, 2 * 2 * 2, get it?) or 100, with 3 remainder

    100 == 2^3rd
    +011 (3 remainder) (you can repeat this same process with the remainder, to get the 011.
    ====
    111 in base 2 == 11 in base 10.
    The principle is right, but you got a little mistake.
    The highest power of 2 that "fits into" 11 is indeed 2 * 2 *2.

    But:
    100 are NOT equal to 2^3.
    This is because we start at 2^0, not 2^1.
    So the correct are:
    1000 = 2^3 = 8.

    13-8 = 3.
    And repeat again: What's the highest base of 2 that fits into 3?
    That's 1, so it's
    2*1, which is equal to:
    10 = 2(in base 10).
    3 -2 = 1.

    What's the highest power of 2 that fits into 1? The highest power is 0, so it's simply:
    1.

    We add everything and get:
    1000
    0010
    0001
    ------
    1011


    and 1011(in base 2) happens to be 11(in base 10)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mats, the java answers
    By Jaqui in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 04-22-2008, 02:12 AM
  2. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  3. First Java Class at college
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 09-29-2004, 10:38 PM
  4. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  5. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM