Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to do binary arithmetic in C?

I am learning C and writing a simple program that will take 2 string values assumed to each be binary numbers and perform an arithmetic operation according to user selection:

  • Add the two values,
  • Subtract input 2 from input 1, or
  • Multiply the two values.

My implementation assumes each character in the string is a binary bit, e.g. char bin5 = "0101";, but it seems too naive an approach to parse through the string a character at a time. Ideally, I would want to work with the binary values directly.

What is the most efficient way to do this in C? Is there a better way to treat the input as binary values rather than scanf() and get each bit from the string?

I did some research but I didn't find any approach that was obviously better from the perspective of a beginner. Any suggestions would be appreciated!

like image 209
jmlane Avatar asked Mar 23 '09 05:03

jmlane


People also ask

What is binary arithmetic explain with example?

1 + 0 = 1. 1 + 1 = 0 (carry 1 to the next significant bit) An example will help us to understand the addition process. Let us take two binary numbers 10001001 and 10010101. example of binary arithmetic clearly explains the binary addition operation, the carried 1 is shown on the upper side of the operands.

What is a binary arithmetic operators?

There are six binary arithmetic operators: addition, subtraction, multiplication, exponentiation (**), division, and modulus (%).


1 Answers

Advice:
There's not much that's obviously better than marching through the string a character at a time and making sure the user entered only ones and zeros. Keep in mind that even though you could write a really fast assembly routine if you assume everything is 1 or 0, you don't really want to do that. The user could enter anything, and you'd like to be able to tell them if they screwed up or not.

It's true that this seems mind-bogglingly slow compared to the couple cycles it probably takes to add the actual numbers, but does it really matter if you get your answer in a nanosecond or a millisecond? Humans can only detect 30 milliseconds of latency anyway.

Finally, it already takes far longer to get input from the user and write output to the screen than it does to parse the string or add the numbers, so your algorithm is hardly the bottleneck here. Save your fancy optimizations for things that are actually computationally intensive :-).

What you should focus on here is making the task less manpower-intensive. And, it turns out someone already did that for you.

Solution:
Take a look at the strtol() manpage:

long strtol(const char *nptr, char **endptr, int base);

This will let you convert a string (nptr) in any base to a long. It checks errors, too. Sample usage for converting a binary string:

#include <stdlib.h>

char buf[MAX_BUF];
get_some_input(buf);

char *err;
long number = strtol(buf, &err, 2);
if (*err) {
    // bad input: try again?
} else {
    // number is now a long converted from a valid binary string.
}

Supplying base 2 tells strtol to convert binary literals.

like image 77
Todd Gamblin Avatar answered Sep 21 '22 00:09

Todd Gamblin