109 votes
Accepted

Why is addition as fast as bit-wise operations in modern processors?

Addition is fast because CPU designers have put in the circuitry needed to make it fast. It does take significantly more gates than bitwise operations, but it is frequent enough that CPU designers ...
D.W.'s user avatar
  • 156k
47 votes
Accepted

How can I multiply a binary representation by ten using logic gates?

I assume that the task is to compute $mul(10, a)= 10a$. You don't need to do multiplication. A single binary adder is enough since $$10a = 2^3a + 2a$$ meaning you add one-time left-shifted $a$ to 3-...
fade2black's user avatar
  • 9,757
45 votes

Signed and unsigned numbers

Short version: it doesn't know. There's no way to tell. If 1111 represents -7, then you have a sign-magnitude representation, where the first bit is the sign and ...
Draconis's user avatar
  • 7,048
43 votes

Why is addition as fast as bit-wise operations in modern processors?

There are several aspects. The relative cost of a bitwise operation and an addition. A naive adder will have a gate-depth which depend linearly of the width of the word. There are alternative ...
AProgrammer's user avatar
  • 3,024
30 votes
Accepted

Computing exam averages in less than linear time

To compute the exact mean (no confidence interval or estimate) of each exam, you must at least observe every student's exam score. This takes $\Omega(r)$ per exam. There are $c$ exams you must do this ...
ryan's user avatar
  • 4,431
26 votes

Why is addition as fast as bit-wise operations in modern processors?

CPUs operate in cycles. At each cycle, something happens. Usually, an instruction takes more cycles to execute, but multiple instructions are executed at the same time, in different states. For ...
Paul92's user avatar
  • 528
20 votes
Accepted

Inequality caused by float inaccuracy

In typical floating point implementations, the result of a single operation is produced as if the operation was performed with infinite precision, and then rounded to the nearest floating-point number....
gnasher729's user avatar
  • 28.4k
20 votes
Accepted

Time complexity of addition

If your algorithm uses asymptotically less than $n$ time, then it does not have enough time to read all the digits of the numbers it is adding. You are to imagine you are handling very large numbers (...
Lieuwe Vinkhuijzen's user avatar
19 votes
Accepted

How does 0 have two values in one's complement?

In 1's complement you just invert all the bits. Consider these 2 examples (assuming 8 bits): $4 = 00000100$, so $-4= 11111011$ $0 = 00000000$, so $-0=11111111$. So you have 2 ways to represent ...
abc's user avatar
  • 1,625
17 votes
Accepted

Signed and unsigned numbers

The short and simple answer is: it doesn't. No modern mainstream CPU ISA works the way you think it does. For the CPU, it's just a bit pattern. It's up to you, the programmer, to keep track of what ...
Jörg W Mittag's user avatar
13 votes

Why is addition as fast as bit-wise operations in modern processors?

Processors are clocked, so even if some instructions can clearly be done faster than others, they may well take the same number of cycles. You'll probably find that the circuitry required to ...
James Hollis's user avatar
12 votes

How to calculate sum of binomial coefficients efficiently?

Hint: Use Lucas's theorem. In general, any time a programming contest problem wants you to compute something mod $p$, check for opportunities to reduce everything mod $p$ before doing any further ...
D.W.'s user avatar
  • 156k
12 votes

Why is addition as fast as bit-wise operations in modern processors?

Addition is important enough to not have it wait for a carry bit to ripple through a 64-bit accumulator: the term for that is a carry-lookahead adder and they are basically part of 8-bit CPUs (and ...
user72735's user avatar
  • 121
11 votes

The math behind converting from any base to any base without going through base 10?

This is a refactoring (Python 3) of Andrej's code. While in Andrej's code numbers are represented through a list of digits (scalars), in the following code numbers are represented through a list of ...
mmj's user avatar
  • 211
11 votes
Accepted

Minimal basis for set of binary vectors using XOR

If you treat your vectors as over the field $GF(2)$ rather than over the set $\{0,1\}$, then what you ask is to find a basis for the span of a set of vectors. This is a well-studied problem in linear ...
Yuval Filmus's user avatar
11 votes

How can I multiply a binary representation by ten using logic gates?

Multiplying by 10 is the same as multiplying by $(1010)_2$. To multiply a binary number $x$ by 10, we thus just have to add $x0$ and $x000$. For example, $6 \times 10 = 60$ is implemented by $$ \begin{...
Yuval Filmus's user avatar
10 votes

Why is addition as fast as bit-wise operations in modern processors?

I think you'd be hard pressed to find a processor that had addition taking more cycles than a bitwise operation. Partly because most processors must carry out at least one addition per instruction ...
pjc50's user avatar
  • 411
10 votes

Signed and unsigned numbers

One of the great advantages of two’s-complement math, which all modern architectures use, is that the addition and subtraction instructions are exactly the same for both signed and unsigned operands. ...
Davislor's user avatar
  • 1,241
9 votes
Accepted

Complexity of Integer Division

Wikipedia has a nice page about the complexity of mathematical operations, and there is also a dedicated page about division. Asymptotically, division has the same complexity as multiplication. The ...
Yuval Filmus's user avatar
9 votes

Why is addition as fast as bit-wise operations in modern processors?

At the gate level, you are correct that it takes more work to do addition, and thus takes longer. However, that cost is sufficiently trivial that doesn't matter. Modern processors are clocked. You ...
Cort Ammon's user avatar
  • 3,241
9 votes
Accepted

Arithmetic network to compute floor of binary logarithm

It can be done with a fairly simple log-depth circuit without resorting to such hacks that only really make sense in software. The "position of most significant set bit" function ...
harold's user avatar
  • 2,018
8 votes

Full Adder vs. Half Adder

The C's are not the same, but your statement about the AND gate is right. Unfortunately, the people who drew your full-adder decided to save space instead of maximizing readability. This image does a ...
Cort Ammon's user avatar
  • 3,241
8 votes
Accepted

Complexity of multiplication

Addition of a number of size $n$ takes time $O(n)$. Don't confuse a number and its encoding size, which is logarithmically smaller. When multiplying an $n$-bit integer $a$ by an $n$-bit integer $b$ ...
Yuval Filmus's user avatar
8 votes
Accepted

XOR two numbers

Bitwise operations like (bitwise) AND, OR, and XOR don't make much sense from the perspective of decimal expansion. They do make some sense in bases which are powers of 2 like hexadecimal, since in ...
Yuval Filmus's user avatar
8 votes
Accepted

Proving that $A \vee (\neg A \wedge B) \equiv A \vee B$

Note that $\qquad A \lor (B \land C) \equiv (A \lor B) \land (A \lor C)$; you can "multiply out". Add in $\qquad (A \lor \lnot A) \land B \equiv B$ and you are done.
Raphael's user avatar
  • 72.1k
8 votes

Why is addition as fast as bit-wise operations in modern processors?

Modern processors are clocked: Every operation takes some integral number of clock cycles. The designers of the processor determine the length of a clock cycle. There are two considerations there: One,...
gnasher729's user avatar
  • 28.4k
8 votes
Accepted

Number of FLOPs (floating point operations) for exponentiation

Assuming multiplication between two numbers use one FLOP, the number of operations for $x^n$ will be $n-1$. However, is there a faster way to do this ... There most certainly is a faster way to do ...
David Hammen's user avatar
8 votes

DFA that accepts strings whose 10th symbol from the right end is 1

It is easy to prove that you need at least $2^{10}$ states. Suppose you need fewer states. Then after feeding $2^{10}$ different sequences of length ten, there exist two sequences ending in the same ...
Zirui Wang's user avatar
7 votes
Accepted

Is it possible to compute -12 (decimal) in 4 bits binary

No, it's not possible, at least using the standard representations. An unsigned $n$-bit number can represent any integer in the interval $[0, 2^{n} - 1]$. A signed $n$-bit number using two's ...
DylanSp's user avatar
  • 865
7 votes

XOR two numbers

There are only 16 distinct binary operations possible for $a$ op $b$, i.e., $0, 1, a, b, \overline{a}, \overline{b}, ab, a\overline{b}, \overline{a}b, \overline{a}\overline{b}, a+b, a+\overline{b}, \...
Sarvottamananda's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible