Thread: Need some help

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    Need some help

    hello everyone
    i need code which converts any number between -128 to 127 into binary
    this is not my homework but a small part of a task assigned to me which states i can use the internet for help
    any help appreciated
    m600

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i need code which converts any number between -128 to 127 into binary
    What exactly do you mean by that? For example, are you trying to print out, or store as a string, a binary representation of the number? Also, what signed binary representation do you have in mind, e.g., two's complement?

    Quote Originally Posted by m600
    this is not my homework but a small part of a task assigned to me which states i can use the internet for help
    That is, it is homework, whether it is graded or not graded, whether you are being paid for it or it is just a challenge by a friend made over dinner.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    ok i need it to convert the number into a binary representation in 8 bits, so 5 would be 00000101
    thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What would -1 be?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    2's compliment so you add 256 to any minus number, then convert it

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good. I suggest that you take a look at std::bitset.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    ok well this is the code i have so far but it wont let me use scanf which is commented out atm
    Code:
    #include <stdio.h> 
    #include <DBOS\GRAPHICS.H>
    #include <STDLIB.H>
    
    int main() 
    { 
        int input=100; 
        int bin_no[8]={0,0,0,0,0,0,0,0},i=7; 
         
         printf("enter number to be converted\n");
         //scanf("%d",&input);
         
        if(-128<input && input<128)
        { 
          if(input<0)
          {
         		input= input + 256;
          }
            do 
            { 
                bin_no[i] = input % 2; 
                input = input / 2; 
                i--; 
             }while(input!=0); 
         
            for(i=0 ; i<8 ; i++) 
            {       
                printf("%d", bin_no[i]); 
            } 
    
           
            printf("\n"); 
        } 
        system("pause"); 
        return 0; 
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this supposed to be C or C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    c++ .............................

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by m600
    c++ .............................
    Do you have any special reason for using C-style I/O instead of C++-style I/O then? Furthermore, you do not need <DBOS\GRAPHICS.H> here, so leave it out.

    If you had taken my suggestion of std::bitset, you could come up with a solution along these lines:
    Code:
    #include <iostream>
    #include <bitset>
    
    int main()
    {
        using namespace std;
        cout << "Enter an integer in the range [-128, 127] to be converted: ";
        int input;
        cin >> input;
        cout << input << " in 8-bit two's complement binary: "
             << bitset<8>((input < 0) ? (input + 256) : input) << endl;
    }
    Of course, you should apply your input error checking as you did in your current code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Kirby-le-Soken, Essex, UK
    Posts
    6
    Try this - it works - C source

    Code:
    #include <stdio.h>
     
    void dec2bin(long decimal, char *binary);
     
    int main()
    {
      long decimal;
      char binary[80];
          
      printf("\n\n Enter an integer value : ");
      scanf("%ld",&decimal);
      dec2bin(decimal,binary);
      printf("\n The binary value of %ld is %s \n",decimal,binary);
      
     
      getchar();  // trap enter 
      getchar();  // wait
      return 0;
    }
     
    //
    // accepts a decimal integer and returns a binary coded string
    //
    void dec2bin(long decimal, char *binary)
    {
      int  k = 0, n = 0;
      int  neg_flag = 0;
      int  remain;
      int  old_decimal;  // for test
      char temp[80];
     
      // take care of negative input
      if (decimal < 0)
      {      
        decimal = -decimal;
        neg_flag = 1;
      }
      do 
      {
        old_decimal = decimal;   // for test
        remain    = decimal % 2;
        // whittle down the decimal number
        decimal   = decimal / 2;
        // this is a test to show the action
        printf("%d/2 = %d  remainder = %d\n", old_decimal, decimal, remain);
        // converts digit 0 or 1 to character '0' or '1'
        temp[k++] = remain + '0';
      } while (decimal > 0);
     
      if (neg_flag)
        temp[k++] = '-';       // add - sign
      else
        temp[k++] = ' ';       // space
     
      // reverse the spelling
      while (k >= 0)
        binary[n++] = temp[--k];
     
      binary[n-1] = 0;         // end with NULL
    }

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    18
    Here is my full c++ code-like tutorial for you, connecting everything and even adding it all up for ya.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	int N = 123; // 32-bit integer.
    
    	for (int j = 0; j < 32; ++j)
    	{
    		// Shift the bits of N over to the right one at a time.
    		// This loop takes 'j' from 0 to 31. N >> j
    		cout << ((N >> j) & 0x1); 
    		// The test looks to see if the first bit of (N>>j) is 'on' or 'off'.
    		// The test returns true for on; 1, False for off; 0.
    	}
    
    	// Output:
    	// 11011110000000000000000000000000
    	// (32 bits)
    
    	// 123 = 1101 1110 0000 0000  0000 0000 0000 0000
    
    	// 1     1 = 1
    	// 2     1 = 2
    	// 4     0 = 0
    	// 8     1 = 8
    	// 16    1 = 16
    	// 32    1 = 32
    	// 64    1 = 64
    	// 128   0 = 0
    	// ... = 0
    
    	// 1 + 2 + 8 + 16 + 32 + 64 = 123
    
    	// Success! :)
    
    	return 1;
    }
    Last edited by syneii; 11-18-2010 at 05:02 PM.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Spoon-feeding central!

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rags_to_riches
    Spoon-feeding central!
    m600 has already posted code in post #7 that looks like more or less a correct implementation, which is why I have no qualms about posting a proper C++ example. I don't know why John Layton bothered posting a C example along the same lines, though syneii's example at least demonstrates a slightly different idea which requires some adaptation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Gee, I did in fact miss that he OP posted code. Oops, sorry about that!

Popular pages Recent additions subscribe to a feed