Skip to main content

Timeline for Binary adder implemented in Rust

Current License: CC BY-SA 4.0

8 events
when toggle format what by license comment
Mar 24, 2023 at 0:47 comment added Peter Cordes If you're manually simulating a ripple-carry adder internals, you can keep it simple and do each bit separately, or derive what the carry-in must have been for each bit from the sum and the two inputs, and use that to calculate the carry-out. So you can get 64 sum bits and 64 carry-out bits stored in two u64s, calculated with only one adc and a few boolean operations and maybe a shift, @theonlygusti. That's a very different design than what you went for, so IDK if I should post that as a code-review answer, especially without taking the time to actually write it in Rust.
Mar 24, 2023 at 0:43 comment added Peter Cordes In languages like C where you don't have convenient access to the carry-out of a full-width addition, or the ability to feed it a carry-in, you might do like Python and use 30-bit chunks in 32-bit integers, so there's room for adding integers and having the carry-out in the uint32_t. But in languages like Rust that are better at this, you'd use let (sum, carry_out) = left.carrying_add(right, carry_in) - doc.rust-lang.org/std/primitive.u32.html#method.carrying_add (carrying_add is a new API.)
Mar 24, 2023 at 0:35 comment added Peter Cordes The match could be entirely replaced with the boolean expressions for the output bits: it's a full adder (en.wikipedia.org/wiki/Adder_(electronics)#Full_adder), so sum = left ^ right ^ carry_in; and carry_out = ((left ^ right)&carry_in) | (left & right);. Or more simply, use the low 2 bits of a u8 for integer addition and extract them: sum = a+b+carry_in; carry_out = sum >> 1; (bit #1) sum &= 1; (bit #0). When doing extended / arbitrary precision integer math, you can use fixed-width addition as a building block, @theonlygusti. Normally 32 or 64-bit chunks!
Mar 23, 2023 at 23:00 comment added cafce25 It requires both left and right to be of the same type, you couldn't for example pass in one forward and one reversed iterator over the same collection.
Mar 23, 2023 at 22:59 history edited cafce25 CC BY-SA 4.0
added 729 characters in body
Mar 23, 2023 at 22:48 comment added user98809 Thanks for the helpful. I actually tried to make a general zip_longest. What about it currently makes it specific?
S Mar 23, 2023 at 22:39 review First answers
Mar 23, 2023 at 23:13
S Mar 23, 2023 at 22:39 history answered cafce25 CC BY-SA 4.0