Question

program in Java or C We read a number i and print all the bits in...

program in Java or C

We read a number i and print all the bits in the binary representation of i+1 strictly to the right of the first 1. One option to of doing that is with a recursive function that checks if its argument is strictly greater than 1 and, if so, calls itself on j right-shifted 1 bit and then prints j mod 2.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// i have attached here the text and screenshot of the running code (recursive one).

// if you are satisfied with my contribution please give a like and if there is any problem please comment.
#include <stdio.h>
void input(int i)
{
printBinary(i+1);
}
void printBinary(int i)
{
if(i<=0)return;
printBinary(i>>1);
printf("%d",i&1);
}
int main()
{
input(9);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
program in Java or C We read a number i and print all the bits in...
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
  • and then print their average. Write a C program that accepts a string of text from...

    and then print their average. Write a C program that accepts a string of text from the user and prints back the string without any of the vowels. For example, if the user entered "Hello, world", the program should print "Hll, wrld" Write a C program that accepts an integer in decimal from the user and prints the number of 1' bits in the integers binary representation. For example, if the user entered 14, the program should print 3 because...

  • How do I write a C program called binary that takes a single command line argument,...

    How do I write a C program called binary that takes a single command line argument, and integer, in decimal, and prints out the binary representation of the number with 64 bits. The argument must be stored as a long. Use atol to convert the command line argument. Include a function char *binary(long n, char *b) { ... } that stores the binary representation in the string b with '0's and '1's. It is the responsibility of the calling program...

  • Please answer ALL parts completely-- answer with clear explanation will get a thumbs up! Problem 2:...

    Please answer ALL parts completely-- answer with clear explanation will get a thumbs up! Problem 2: Programming [2 points] Consider the following pseudo code where a function has been defined for a non negative integer x. Note that the function calls itself (within the while loop) making it a recursive function. def func (x) print x while i<x: i = i+1 func (x-1) (a) How many times does the program print if we call func (0) and func (1), respectively?...

  • 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 =...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • Write a C program that uses the bitwise shift operators to shift the bits to the...

    Write a C program that uses the bitwise shift operators to shift the bits to the right >> or the left > m; /* This shifts m bits to the right, and the m least significant bits are lost.*/ The following statements are the same. num = num >> 3; num >>= 3; Show the operation in binary by calling the following function as defined in 3.1, void to_binary(unsigned int n); The function converts decimal to binary and outputs the...

  • Create a program (java): that reads a 8-bit String input, must be read in as such....

    Create a program (java): that reads a 8-bit String input, must be read in as such. Then convert that string of 1s and 0s to decimal as if it were encoded using the following representations: Unsigned 1's Complement 2's Complement Q(4.4) (2's complement) 1 bit (sign) - 3 bits (exponent: use bias) - 4 bits (significand), implied binary point w/ 1. Also check for 0.... Do not use helper functions, must be done iterating string and modifying an int/long value....

  • CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from...

    CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from row and // column wise sorted matrix #include<iostream> #include<climits> using namespace std;    #define INF INT_MAX #define N 4    // A utility function to youngify a Young Tableau. This is different // from standard youngify. It assumes that the value at mat[0][0] is // infinite. void youngify(int mat[][N], int i, int j) {     // Find the values at down and right sides of...

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • I need help writing this program in C++. Thanks so much for your help in advance...

    I need help writing this program in C++. Thanks so much for your help in advance Function 1: Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, ... 50. Use recursion to calculate the sum. Demonstrate the function in a program. A sample...

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