469,898 Members | 1,417 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Help converting an integer to binary digit

31
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
Oct 24 '06 #1
9 7491
Banfa
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. #include<limits.h>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     unsigned num = <SomeValue>;
  9.     unsigned mask = 1 << ((CHAR_BIT*sizeof unsigned)-1);
  10.  
  11.     if (num & mask)
  12.     {
  13.         cout << "1";
  14.     }
  15.     else
  16.     {
  17.         cout << "0";
  18.     }
  19.  
  20.     return 0;
  21. }
  22.  
Oct 24 '06 #2
hapa
31
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
Oct 25 '06 #3
hapa
31
one more thing is this LSB and MSB
some what like little endian and big endian
Oct 25 '06 #4
Banfa
9,065 Expert Mod 8TB
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 0000000011111111)

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 1111111100000000)

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.
Oct 25 '06 #5
hapa
31
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
Oct 25 '06 #6
Banfa
9,065 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1. AND TRUTH TABLE
  2. Input 1 Input 2|Result
  3. ----------------------
  4.    1       1   |  1
  5.    1       0   |  0
  6.    0       1   |  0
  7.    0       0   |  0
  8.  
  9. OR TRUTH TABLE
  10. Input 1 Input 2|Result
  11. ----------------------
  12.    1       1   |  1
  13.    1       0   |  1
  14.    0       1   |  1
  15.    0       0   |  0
  16.  
  17. XOR TRUTH TABLE
  18. Input 1 Input 2|Result
  19. ----------------------
  20.    1       1   |  0
  21.    1       0   |  1
  22.    0       1   |  1
  23.    0       0   |  0
  24.  
  25. NOT TRUTH TABLE
  26. Input|Result
  27. -------------
  28.    1  |  0
  29.    0  |  1
  30.  
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).
Oct 25 '06 #7
hapa
31
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
Oct 25 '06 #8
hapa
31
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
Oct 25 '06 #9
Banfa
9,065 Expert Mod 8TB
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.
Oct 25 '06 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Darren | last post: by
6 posts views Thread by Alex Neumann | last post: by
2 posts views Thread by Mariusz Sakowski | last post: by
6 posts views Thread by Madhusudan Singh | last post: by
3 posts views Thread by alyssa | last post: by
4 posts views Thread by ross.oneill | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.