Help converting an integer to binary digit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hapa
    New Member
    • Oct 2006
    • 31

    Help converting an integer to binary digit

    I have a problem in this C++ program That i have written. help me with the problem that i have mentioned below
    I have written this program to convert a integer to binary digit
    problem one is that the binary conversion that i get is inverse i.e

    if the conversion for 16 is
    10000
    but this program will output it as
    00001
    wut function can i use to convert my answer 00001 into 10000
    should i use strings fuction get the length of the line than extact each string and use it to get 10000
    Or is there a easy way that i can use
    but i think the way i did is fine but the problem is that i am getting inverse answer waiting to hear from you thank u

    #include <iostream>
    #include <cmath>
    using namespace std;

    int main ()
    {
    unsigned int a;
    unsigned int y;
    a=16;
    y=a%2;
    a=a/2;
    while((a)!=0)
    {
    cout<<y;
    y=a%2;
    a=a/2;
    }
    cout<<y<<endl;

    return 0;
    }


    If you dont understand any part of my question or my program u can ask me

    I recently got an idea would it be better to use a bitwise operator
    if so can you guide me a bit on using it and the function that i can use
    be more specific in wut fuction can i use as bitwise and how can i use it
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The reason you get the output reversed is that you process the bits least significant bit (LSB) first. To get the output in the correct order you need to process most significant bit (MSB) first.

    You can use bit wise operators, you can take advantage of the fact binary only has 2 digits 0 and 1. You can also interpret these as 0 and NOT 0. You need to set a mask for each bit in turn and test it for 0 or NOT 0 and create output and then shift the mask to the next bit.

    This code tests the MSB of an int

    Code:
    #include<limits.h>
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        unsigned num = <SomeValue>;
        unsigned mask = 1 << ((CHAR_BIT*sizeof unsigned)-1);
    
        if (num & mask)
        {
            cout << "1";
        }
        else
        {
            cout << "0";
        }
    
        return 0;
    }

    Comment

    • hapa
      New Member
      • Oct 2006
      • 31

      #3
      The problem is that the instructor has not thaught us the bitwise operator and i read about bitwise operator on a web page and i am totally blank on how to use them and make a logiacal expression from it
      i have done so much research on this but no where it shows how we can use it in visual.net 2003 C++
      if you can just explain me a little banfa

      Comment

      • hapa
        New Member
        • Oct 2006
        • 31

        #4
        one more thing is this LSB and MSB
        some what like little endian and big endian

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by hapa
          one more thing is this LSB and MSB
          some what like little endian and big endian
          No but the are terms that are used is describing big endian and little endian.

          Definitions

          LSB - Least Significant Bit or Least Significant Byte depending on the context it is used in.

          The least significant bit is the one with least value, in an 8 bit value the LSB is the one that holds the units column, if the LSB is 1 and all the others are 0 the value will be 1 (binary 00000001).

          The Least Significant Byte is the byte having the least effect in a multi-byte integer on the value of the integer. In a 2 byte integer the with the LSB set to all ones and the rest of the bits set to 0 the value will be 255 (binary 000000001111111 1)

          MSB - Most Significant Bit or Most Significant Byte depending on the context it is used in.

          The most significant bit is the one with greatest value, in an 8 bit value the MSB is the one that holds the 128s (2 to the power 7) column, if the MSB is 1 and all the others are 0 the value will be 128 (binary 10000000).

          The Most Significant Byte is the byte having the most effect in a multi-byte integer on the value of the integer. In a 2 byte integer the with the MSB set to all ones and the rest of the bits set to 0 the value will be 65280 (binary 111111110000000 0)

          Big Endian and Little Endian describe 2 (of several) methods of ordering the bytes in a multi byte integer in physical memory that any processor may use.

          In Big Endian the MSB comes first in memory, the LSB comes last.

          In Little Endian the LSB comes first in memory, the MSB comes last.

          Comment

          • hapa
            New Member
            • Oct 2006
            • 31

            #6
            Tell me one thing can we get the output the way we want suppose my value for decimal 16 in binary form is 10000
            but if i want my output to be in a form of 32 bit is this possible or am i suppose to use something like setfill()
            or is that totally wrong ( its real bad programming isnt it)
            suppose i want to get my output this way

            0000 0000 0000 0000 0000 0000 0001 0000

            can i get this with a function or use setfill ( i know i sound like a stupid person sorry for that)
            but atleast i am trying to learn on my own

            reply please banfa

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by hapa
              The problem is that the instructor has not thaught us the bitwise operator and i read about bitwise operator on a web page and i am totally blank on how to use them and make a logiacal expression from it
              i have done so much research on this but no where it shows how we can use it in visual.net 2003 C++
              if you can just explain me a little banfa
              OK so you understand that

              & - AND bitwise operator
              | - OR bitwise operator
              ^ - XOR bitwise operator
              ~ - NOT bitwise operator


              Do you know the truth tables for these operations? They are
              Code:
              AND TRUTH TABLE
              Input 1 Input 2|Result
              ----------------------
                 1       1   |  1
                 1       0   |  0
                 0       1   |  0
                 0       0   |  0
              
              OR TRUTH TABLE
              Input 1 Input 2|Result
              ----------------------
                 1       1   |  1
                 1       0   |  1
                 0       1   |  1
                 0       0   |  0
              
              XOR TRUTH TABLE
              Input 1 Input 2|Result
              ----------------------
                 1       1   |  0
                 1       0   |  1
                 0       1   |  1
                 0       0   |  0
              
              NOT TRUTH TABLE
              Input|Result
              -------------
                 1  |  0
                 0  |  1
              These tables tell you what happens when you use a given bitwise operation on 2 bits. So from the AND table 1 AND 1 = 1, 1 AND 0 = 0, 0 AND 1 = 0 and 0 AND 0 = 0

              When you use the operator on 2 variables then it uses the bits from the same bit position of each variable to create the bit in the result. In bitwise operations there is no carry, the result at 1 bit position does not effect any other bit postions so for

              unsigned a,b,r;

              r = a & b;

              bit 0 of r is the result of (bit 0 of a AND bit 0 of b)
              bit 1 of r is the result of (bit 1 of a AND bit 1 of b)

              etc.

              Ifn the code I posted

              if (num & mask)

              num is the unknown I am testing, mask is the known I am using to test it.

              I have arranged that mask would only have 1 bit set, (the MSB as it happens), since

              x AND 0 = 0 for any x (check the truth table) I know that except for the MSB the all bits in the result of (num & mask) will be 0 because the bits in the mask are 0.

              x AND 1 = x for any x (check the truth table) the I know that the MSB in the result of (num & mask) will have the value of the MSB of num.

              since all the other bits in the result of (num & mask) are 0 the result of (num & mask) is 0 if the MSB of num = 0 and the result of (num & mask) is NOT 0 if the MSB of num = 1.

              and that is how the output is created.

              (and just to let you know is gone 2am here so I am going to bed, since I have to visit a client tomorrow you are unlikely to get my next reply for 19 - 20 hours).

              Comment

              • hapa
                New Member
                • Oct 2006
                • 31

                #8
                Thank you i am finally getting the hang of it you are a great help

                one more thing can you tell me about getting my output in 32 bit form where as the output that i am getting is only depending on the number that i enter and only its binary representation but i want to get it in 32 bit no matter how small the number it

                am i suppose to use bitwise operator for this purpose too

                Comment

                • hapa
                  New Member
                  • Oct 2006
                  • 31

                  #9
                  I am still learning and it is 5:28 AM and i have to go to the university at 8:00am

                  I cannot thank you enough for you have explained the LSB and MSB and explaied about the bitwise operator
                  i tried to understand it but every thing was just going over my head thank u
                  thank u
                  thank u
                  thank u

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by hapa
                    I am still learning and it is 5:28 AM and i have to go to the university at 8:00am

                    I cannot thank you enough for you have explained the LSB and MSB and explaied about the bitwise operator
                    i tried to understand it but every thing was just going over my head
                    That is because it was 5:28am, examine it at a more socialiable hour. Let me know if there are still bits you don't understand.

                    Comment

                    Working...