Question

1. Implement an algorithm to convert binary number into an integer. Binary number is represented as...

1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010");

int to_int(const std::string& b) {

}

2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string.

std::string to_binary(int n) {

}

3. Implement a function to check if the number is positive or negative. The function should return true if number is positive.

bool is_positive(int n) {

}

4. Implement an algorithm to print all possible values of 32 bit unsigned integer one bit position set.

Output should look like.

1

2

4

8

16

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using namespace std;

int to_int(const std::string& b) {
    int result = 0;
    for (int i = 0; i < b.length(); ++i) {
        result *= 2;
        result += b[i]-'0';
    }
    return result;
}

std::string to_binary(int n) {
    string ret = "";
    while (n > 0) {
        ret += '0' + (n % 2);
        n /= 2;
    }
    return ret;
}

bool is_positive(int n) {
    return n > 0;
}

void all_values() {
    unsigned int n = 1;
    for (int i = 0; i < 32; ++i) {
        cout << n << endl;
        n <<= 1;
    }
}

int main() {
    cout << to_int("101001") << endl;
    cout << to_binary(41) << endl;
    cout << is_positive(3) << endl;
    cout << is_positive(-5) << endl;
    all_values();
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
1. Implement an algorithm to convert binary number into an integer. Binary number is represented as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4.14 LAB: Convert to binary Write a program that takes in a positive integer as input,...

    4.14 LAB: Convert to binary Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is:...

  • Defining a binary in c language, write the function int binToDec(const int bin[]) to convert an...

    Defining a binary in c language, write the function int binToDec(const int bin[]) to convert an eight-bit unsigned binary number to a nonnegative decimal integer. Do not output the decimal integer in the function. Test your function with interactive input?

  • Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

    Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • using assembly 8086 , Convert a binary string to decimal. Accept buffered user input in the...

    using assembly 8086 , Convert a binary string to decimal. Accept buffered user input in the form of a 16-bit binary string. Convert to unsigned integer and print to the screen. if the user enters less than 16 bits, you may pad the binary number with zeros on the left. Sample Execution: Enter a 16-bit binary number: 1001111001011100 The decimal unsigned integer equivalent is 40540 Enter a 16-bit binary number: 01100 The decimal unsigned integer equivalent is 12

  • Python Programing : Convert to binary - functions Write a program that takes in a positive...

    Python Programing : Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a...

  • 1. In ANSII standard C++, there is no library function to convert an integer to a...

    1. In ANSII standard C++, there is no library function to convert an integer to a string. Your program should develop such a function. In other words complete the following program using your itos function. (Use of other C functions is prohibitted) // this program will read in an integer and convert it to a string #include <iostream> #include <cstdlib> #include <string> using namespace std; string itos(int n) { } int main(int argc, char* argv[]){ int n = atoi(argv[1]); cout...

  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • Short Answer Given the following class declaration for a binary string/integer pair (the binary n...

    ANSWER IN C++ Constructors Short Answer Given the following class declaration for a binary string/integer pair (the binary number, in the form of a string. should be the binary form of the integer): class binint public: int num; // Note this is an address! string *bin; // Note that this is an address! binint (int n); binint (string s); void convertitos(int n); void convertstoi(string s); II NOTE: you do not have to do anything // with this method you can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT