Thread: Decimal to 16 bit unsigned?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Decimal to 16 bit unsigned?

    I can't seem to find any tutorial on the web that explains how to convert a decimal to a 16 bit unsigned binary integer and vice versa done with hand.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you know what "binary" means -- as in, powers of two? If so, then you're done.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    http://en.wikipedia.org/wiki/Binary_numeral_system

    It may not explain precisely how to do this for a 16-bit number, but the principle that you apply is the same regardless of number of bits.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think I understand the question, so let me reask his question in a non-uninformed way:

    Quote Originally Posted by blurx
    Greetings C Programming.com patrons,

    I am wondering if someone knows how to convert a 16-bit scalar into a binary string by hand. Any links or input would be greatly appreciated.

    -blurx
    Well blurx, that is not a hard thing to do at all, actually. Here is some sample code, since you formulated your question in such a cordial way:

    Example:
    Code:
    void int16tobstr(short x, char s[17])
    {
      int bit;
    
      for(bit = 0; bit < 16; ++bit)
        *s++ = '0' + !!(x & (1 << bit));
     *s = 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    I realized that it was just more powers of 2, since I got confused with 16 digits compared to 8.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    16 digits means you were dealing with a 16bit number. 32 digits means a 32 bit number. So when someone says a 64-bit number, that means that its binary digits could be quantified as 64.

    Binary digIT is what a bit is anyhow. So now some information you have in your brain is going to go full circle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. unable to recieve UDP packet
    By caroundw5h in forum Networking/Device Communication
    Replies: 15
    Last Post: 09-19-2007, 11:11 AM
  3. Converting an unsigned int to to unsigned long long (64 bit)
    By ninjacookies in forum C Programming
    Replies: 18
    Last Post: 02-11-2005, 12:09 PM
  4. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM