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

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

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

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

  • Part 1: Write a C program that takes an integer command line argument n, spawns n...

    Part 1: Write a C program that takes an integer command line argument n, spawns n processes that will each generate a random numbers between -100 and 100, and then computes and prints out the sum of these random numbers. Each process needs to print out the random number it generates. name the program 003_2.c

  • Write a C++ program that takes two numbers from the command line and perform and arithmetic...

    Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

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