Question

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 (which will be main) to allocate a string of large enough size (65) to hold the string. The function will return b. This allows it to be called like this: printf("%s\n", binary(n, b));
You must use bit operations, in particular, anding with a mask, to examine the ith bit.

Sample runs:

calvin 09:48:30 > ./binary
usage: binary n
calvin 09:48:35 > ./binary 100
0000000000000000000000000000000000000000000000000000000001100100
calvin 09:48:42 > ./binary 0
0000000000000000000000000000000000000000000000000000000000000000

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

Here's the code:

#include<bits/stdc++.h>
using namespace std;

char *binary(long n, char *b) {
int curr_idx = 63;
while(n){
if(n%2 == 0){
b[curr_idx] = '0';
}
else{
b[curr_idx] = '1';
}
n = n/2;
curr_idx--;
}
return b;
}

int main(int argc, char * argv[]){
long n = atol(argv[1]);
char b[65];
// fill with 0's initially
for(int i = 0; i < 65; i++){
b[i] = '0';
}
b[64] = '\0'; // last null terminator
printf("%s\n", binary(n, b));
}

Add a comment
Answer #2

Hello, I have done the question and it is working perfectly fine. I have tried to name the variables in a proper manner to give you a better understanding for the code. I am also attaching the sample input/output file as well. It took me a lot of time to answer this. Please give me an upvote.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* long_to_binary(unsigned long k)

{
static char c[65];
c[0] = '\0';

unsigned long val;
for (val = 1UL << (sizeof(long)*8-1); val > 0; val >>= 1)
{   
strcat(c, ((k & val) == val) ? "1" : "0");
}
return c;
}

int main(int argc, char *argv[]){
if(argc<=1){
printf("usage: binary n");
return 0;
}
long number = atol(argv[1]);
  
//We cannot use a signed long to calculate the binary values so we have to typecast the value.
printf("%s\n",long_to_binary((unsigned long)number));
}


Sample input/output file:-

Add a comment
Know the answer?
Add Answer to:
How do I write a C program called binary that takes a single command line argument,...
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
  • Write a C program called test that takes one command line argument, an integer N. When...

    Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • In C Programming Adding to your program in part A, go through the command line arguments...

    In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A:             Three two one And then it would display   The smallest string was:...

  • Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusi...

    Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string (‘A’<->’Z’, ‘B’<->’Y’, ‘C’<->’X’, etc). You should divide this conversion task among the n threads as evenly as possible. Print out the string both before...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • Using java write a program that takes a single, positive integer, n as a command-line argument....

    Using java write a program that takes a single, positive integer, n as a command-line argument. The program should then plot n evenly spaced points around a circle

  • Write a program that determines if a string (passed as a command-line argument) has all unique...

    Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique. Special requirements: You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings). You may have only one string (the one received as the command-line argument). You may have one...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Java!!! Write a program that takes a file name as its command line argument. It assumes...

    Java!!! Write a program that takes a file name as its command line argument. It assumes the file is a binary data file and checks the first 4 bytes of the file to see whether they contain the integer −889275714. It outputs “yes” or “no” or an error message if there was an IOException generated. (Trivia question: Why test for that particular value? Hint: Try it on several .class files.)

  • Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained...

    Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained in a1q4.c and perform the following tasks in your program’s main() function: (8 marks) • Declare and initialize at least one variable of these types: char, int, unsigned int, float, double and long double. When initializing your variables, ensure the constants you use are of the same type as your variable. See the “Constants” section of Topic 7 for more information. • Use printf(3)...

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