binary numbers

This is a discussion on binary numbers within the C Programming forums, part of the General Programming Boards category; how do you convert a binary number to its decimal equivalent? if someone could explain this to me in English ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    Question binary numbers

    how do you convert a binary number to its decimal equivalent? if someone could explain this to me in English that would be helpful. I'm not quite sure what a binary number is or how to go about creating a program that does the above. any feedback would be appreciated.
    Watshamacalit

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    99

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Binary numbers do take a bit of getting used to, but it's not too bad once one figures out what's going on. This is not scientific by any means.

    Start with 1 byte or 8 bits.

    1 means a bit is set (on)
    0 means a bit is not set (off)

    bits 0-7

    bit 0 = 1..... 2^0
    bit 1 = 2..... 2^1
    bit 2 = 4..... 2^2
    bit 3 = 8..... 2^3
    bit 4 = 16.... 2^4
    bit 5 = 32.... 2^5
    bit 6 = 64.... 2^6
    bit 7 = 128.. 2^7

    A bit pattern is read from right to left, so you must remember to fill in all missing left side bits with zeros (as you become familiar with the concept, you won't need to do that.)

    1101 read right to left is

    bit 0 set = 1
    bit 1 not set = 0
    bit 2 set = 1
    bit 3 set = 1

    the full eight bits

    00001101b

    look back at the bits 0-7 and add the set bits together. You get 13 dec. The 7th bit is a sign bit.

    As far as a proggie, that isn't too hard either provided that you keep in mind my example is very basic.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main(void)
    {
       int oneByte[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
    		
       char binary[15]; /* 8 bits + '\0' + overflow margin */
       char buff[9];
       char *SB; /* sign bit */
       int i, j, decimal;
    	
       printf("Enter a binary string up to (8 bits): ");
       gets(binary);
    	
      if(strlen(binary) > 8)
      {
         printf("\n\nInput string greater than eight bits");
       return -1;
      }
    	
      if(binary[0] == '\0')
      {
         printf("\n\nThere is nothing to do.");	 
         return 0; /* not really an error */
      }
    	
      j=0;
    	
      /* reverse string */
    	
      for(i=strlen(binary)-1; i >= 0; i--)
         buff[j++] = binary[i]; 
    		
      /* terminate the reversed string */
    	
      buff[j] = '\0';
    	
      decimal = 0;
    	
      for(i=0; buff[i] != '\0'; i++)
      {
         /* use the switch to find out if the data
    is good */
    		
      switch(buff[i])
      {
         case '0': break;
         case '1': decimal += oneByte[i]; break;
         case '\0': break;
         default : 
         printf("\n\nInvalid entry => %c <= detected in input", buff[i]);
         return -1;
         break;
      };
    		  
      }
    	
      if(strlen(buff) == 8 && buff[7] == '1')
         SB = "(unsigned)";
      else
         SB = "(signed)";
    	
      printf("\n\n%sb is %d dec %s", binary, decimal, SB);	 
    	
      return 0;
    }
    I *think* that will do... technically correct or not there ya go. Good luck.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Any given number can be viewed as a binary, octal, decimal, hexidecimal - whatever. These are just ways of looking at numbers. Your computer can't actually store a number any other way than binary. When the computer was invented, there was no way to feasably store 10 states (decimal) on a single "switch", so bi-state (binary) switches were chosen. Since, like a light bulb, only two states can be represented with a binary switch, the binary number system evolved. If you have two light bulbs, you can store the value 0 or 1. If you have two, a number as high as 3 can be stored. So on. Binary as it turns out is in many respects superior to any other number system.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    Thanks All!

    you've helped me alot! my c class will be over as of thursday...so i wont be posting anymore...

    but thank you all very much for helping me and puting up with my questions...
    Watshamacalit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking if binary numbers are palindromic
    By Beatz in forum C Programming
    Replies: 3
    Last Post: 01-24-2008, 12:49 PM
  2. something about binary numbers in c
    By louis_mine in forum C Programming
    Replies: 1
    Last Post: 02-09-2005, 09:22 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Binary representation of numbers...
    By roc in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2003, 07:42 PM
  5. how to pack 8 x 4 bit binary numbers into a long int?
    By n00bcodezor in forum C Programming
    Replies: 11
    Last Post: 11-19-2001, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21