# Binary to Gray Conversion Program

• uknowwho
In summary, the conversation discusses creating a conversion program from binary to gray and vice versa in C++ without using loops or functions. The speaker is successful in converting 4-bit numbers and asks for suggestions on how to store the bits in a string without spaces. The responder suggests using vectors, but notes that it may be difficult to implement without loops or functions. They also provide a code example for how to handle the input without using loops or functions. The original speaker then asks for a simpler way to do it without getting too complicated, and mentions their basic understanding of loops. The responder suggests downsizing the code example provided and clarifies the speaker's limitations in terms of what they have learned so far.
uknowwho
We just started out C++ and I thought of making a conversion program from binary to gray and vice versa just for fun and to improve my skills

I was successful to make conversions of 4 bits

#include <iostream>

using namespace std;

int main() {

int Choice;
bool g0,g1,g2,g3,b0,b1,b2,b3;

cout<<"For Binary To Gray Code Enter 1." << endl <<"For Gray Code to Binary Enter 2." << endl;;
cin>>Choice;

if(Choice==1){

cout<<"Enter the Binary Bits." << endl;

cin>>b0>>b1>>b2>>b3;

cout<<"Orginal Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;

g3=b3;
g2=b3^b2;
g1=b2^b1;
g0=b1^b0;

cout<<"Converted Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;
}
else if(Choice==2){

cout<<"Enter The Gray Code Bits." << endl;
cout<<"g0: ";
cin>>g0;
cout<<"g1: ";
cin>>g1;
cout<<"g2: ";
cin>>g2;
cout<<"g3: ";
cin>>g3;

cout<<"Orignal Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;

b3=g3;
b2=b3^g2;
b1=b2^g1;
b0=b1^g0;

cout<<"Converted Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;
}

return 0;
}

but what if I want to store the number of bits entered by the user in real time is it possible?
I heard using vectors is more useful in c++ than arrays? Also how to store the bits in a string so that the user can enter the bits in one line without spaces.
We haven't studied loops and making functions as of yet so it would be great If you suggest simpler ideas

Without loops and functions, you are limited to a fixed number of input bits (or an insane amount of if-statements), I think. You can use a string, but I don't see a reasonable method to tell C++ "do this [stringlength] times" without loops or functions.

>> Also how to store the bits in a string
In the same way you can store a single bit in a bool.

uknowwho said:
how to store the bits in a string so that the user can enter the bits in one line without spaces

Code:
#include <string>
#include <iostream>

using namespace std;

int main ()
{
string binarybits;
cout << "Enter the binary bits (without spaces): ";
cin >> binarybits;
cout << "OK, you entered " << binarybits << "." << endl;
return 0;
}

mfb said:
Without loops and functions, you are limited to a fixed number of input bits (or an insane amount of if-statements), I think. You can use a string, but I don't see a reasonable method to tell C++ "do this [stringlength] times" without loops or functions.

>> Also how to store the bits in a string
In the same way you can store a single bit in a bool.

Ok if we were to use loops what the simplist way to do it without getting to complicated

i know the basics of loops

uknowwho said:
Also how to store the bits in a string so that the user can enter the bits in one line without spaces.

This
user can enter the bits in one line without spaces
is a valid requirement, but this
Also how to store the bits in a string
is based on an invalid assuption - you don't need a string for buffering the input when whitespaces are absent, pls consider this:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <cctype>    // iswspace
#include <stdexcept> // logic_error
#include <iterator>  // ostream_iterator
#include <algorithm> // std::copyint main(/*int argc, char* argv[]*/) {
typedef std::vector<bool> bit_vec;
typedef std::ostream_iterator<bit_vec::value_type, char> outp_it;

bit_vec bv;
char c;

try {
while (std::cin.get(c)) {
if ((c == '0') || (c == '1')) {
bv.push_back(bool(c - '0'));
} else if (!iswspace(c)) {
throw std::domain_error(std::string(&c,1).c_str());
}
}
} catch (std::domain_error& e) {
std::cerr << "Invalid literal: " << e.what() << std::endl;
exit(1);
}

std::copy(bv.begin(), bv.end(), outp_it(std::cout));
std::cout << std::endl;

return 0;
}

You better downsize that yourself, from this
uknowwho said:
I heard using vectors is more useful in c++ than arrays? Also how to store the bits in a string so that the user can enter the bits in one line without spaces.
We haven't studied loops and making functions
I cannot get what you're allowed to use - apparent free choice of containers but no loops or functions yet does not appear consistent.

Regards, Solkar

Solkar said:
This
is a valid requirement, but this
is based on an invalid assuption - you don't need a string for buffering the input when whitespaces are absent, pls consider this:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <cctype>    // iswspace
#include <stdexcept> // logic_error
#include <iterator>  // ostream_iterator
#include <algorithm> // std::copy

int main(/*int argc, char* argv[]*/) {
typedef std::vector<bool> bit_vec;
typedef std::ostream_iterator<bit_vec::value_type, char> outp_it;

bit_vec bv;
char c;

try {
while (std::cin.get(c)) {
if ((c == '0') || (c == '1')) {
bv.push_back(bool(c - '0'));
} else if (!iswspace(c)) {
throw std::domain_error(std::string(&c,1).c_str());
}
}
} catch (std::domain_error& e) {
std::cerr << "Invalid literal: " << c << std::endl;
exit(1);
}

std::copy(bv.begin(), bv.end(), outp_it(std::cout));
std::cout << std::endl;

return 0;
}

You better downsize that yourself, from this

I cannot get what you're allowed to use - apparent free choice of containers but no loops or functions yet does not appear consistent.

Regards, Solkar

No we haven'e learned neither of those as of yet(vecors and arrays).I googled for those.

Solkar said:
Code:
 while (std::cin.get(c))
Nonsens, that's meant to be interactive, isn't it?
So scratch that and put
Code:
 while (std::cin.get(c) && c != '\n')

## What is Binary_Gray Conversion?

Binary_Gray Conversion is a method of converting binary numbers to their corresponding Gray code representation. Gray code is a non-weighted code that is used in digital communication systems.

## Why is Binary_Gray Conversion important?

Binary_Gray Conversion is important because it reduces the possibility of errors in digital communication systems. It also simplifies the design of logic circuits and is commonly used in digital signal processing.

## How do you convert binary numbers to Gray code using Binary_Gray Conversion?

To convert a binary number to Gray code using Binary_Gray Conversion, you can use the following steps:

1. Write the binary number as a row of digits.
2. Write a zero to the left of the most significant bit (MSB).
3. Write the MSB of the binary number as the first digit of the Gray code.
4. XOR each pair of adjacent digits, starting from the left, and write the result as the next digit of the Gray code.
5. Repeat step 4 until all digits have been processed.

## What are the advantages of using Binary_Gray Conversion over other methods of binary to Gray code conversion?

One advantage of using Binary_Gray Conversion is that it is a simpler and more efficient method compared to other methods. It also eliminates the need for complex algorithms and reduces the possibility of errors.

## Are there any limitations to using Binary_Gray Conversion?

One limitation of Binary_Gray Conversion is that it can only be used for converting binary numbers to Gray code and not vice versa. Additionally, it may not be suitable for all applications and may require additional processing steps in some cases.

• Programming and Computer Science
Replies
10
Views
1K
• Programming and Computer Science
Replies
235
Views
10K
• Programming and Computer Science
Replies
8
Views
2K
• Programming and Computer Science
Replies
4
Views
5K
• Programming and Computer Science
Replies
3
Views
3K
• Programming and Computer Science
Replies
6
Views
1K
• Programming and Computer Science
Replies
75
Views
4K
• Programming and Computer Science
Replies
14
Views
31K
• Engineering and Comp Sci Homework Help
Replies
2
Views
1K
• Programming and Computer Science
Replies
1
Views
3K