472,362 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,362 software developers and data experts.

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 7702
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

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

Similar topics

9
by: Darren | last post by:
Hi wondering if anyone can help. What i'm trying to do is get a company from a MSSQL database with the COMPANYNO which is a 'uniqueidentifier'. Then with this COMPANYNO I want to reference it to...
6
by: Alex Neumann | last post by:
Hi, I need a function which converts an string to an integer. Currently I have this: bool string2int(char* digit, int& result) { result = 0; if (!(*digit >= '0' && *digit <='9'))
2
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
6
by: Madhusudan Singh | last post by:
Hi I am using binascii.b2a_hex to convert some binary data to hex. The result is a two bit hex representation (i. e., without the leading hex). How do I convert the resulting two bit...
3
by: alyssa | last post by:
Hi guys, May i know how to declare a string of binary data and pass it to the method? For example, int a={10010001120420052314} is it correct? and if i have receive the binary data, may i know...
4
by: ross.oneill | last post by:
Hi, I have a string strcpy(str, "2A"); and I want to grab the char 2 and convert it to an integer. I tried doing this int num = atoi(str);
4
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
14
by: Default User | last post by:
Hi, If I have three 64 bit integers and I want to do this operation on them: x*y/z Lets say that what we are multiplying by (y) is offset by what we are dividing by (z) so that the final...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.