[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

>So you're a programmer, huh? >How about you add two

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 99
Thread images: 7

File: fedora.jpg (188KB, 900x1200px) Image search: [Google]
fedora.jpg
188KB, 900x1200px
>So you're a programmer, huh?
>How about you add two numbers without using addition, bitch.
>>
So, bit shifting?
>>
1 and 1
>>
>>60004208
1 - (-1) isnt technically addition
>>
>>60004208
How about you take off that fake mustache?
>>
>>60004208
>How about you add two numbers without using addition, bitch.
n -(-n)
>>
>>60004208
>How about you add two numbers without using addition, bitch.

With side effects, just to piss the anti-c fags off a little more.
#include <stdio.h>

int add(int x)
{
static int b = 0, s = 0;
int a = 0, t;
if (!s) {
a = b; b = x;
} else {
a = x; t = b;
do {
a ^= b;
b = (a^b) & b;
b <<= 1;
} while (b);
b = t;
}
s = (-~s) & 1;
return a;
}

int main()
{
int r;
add(3);
r = add(4);
printf("3 + 4 = %d\n", r);

add(6);
r = add(7);
printf("6 + 7 = %d\n", r);
return 0;
}
>>
>>60004251
nice
>>
>>60004251
how did you do it this fast
>>
>>60004244
>-(-
Looks like a + to me
>>
>>60004275
I was asked this in an interview half a year ago or something, so I implemented it after the interview and had it lying around.
>>
>>60004260
>>60004275
>>60004251
>>60004289
Cleaned up version, it's not really hard:

int add(int a, int b)
{
do {
a ^= b;
b = (a^b) & b;
b <<= 1;
} while (b);

return a;
}
>>
Nice.

But how many of you can average two ints in C?
>>
>Mi'dama
>>
>>60004208
#define NOTADDITION +

a NOTADDITION b;
>>
>>60004208
>All these plebs using hardware accelerated addition

Did you know there are some really old CPUs that don't even have hardware division and multiplication?
>>
>>60004499
Who cares, this isn't 1963 anymore fgt.
>>
>>60004347
Well, I understand the while loop, just not what's inside!

Actually, I just don't get why the bit operators somehow combine to make addition here.
>>
>>60004456
int avg = a - b/2;
>>
>>60004542
>the average of 2 and 7 is -1
>>
>>60004347
This doesn't work on OpenBSD/sparc64???
>>
>>60004616
Big endian != little endian
>>
add a and b:

-((-a) - b)
>>
int add(int a, int b) {
printf("quick, i need your help! whats %d + %d?", a, b);

int result;
scanf("%d", &result);

puts("thx m8! =)");

return result;
}
>>
>>60004710
I like this one.
>>
File: 1289466419932.jpg (62KB, 543x431px) Image search: [Google]
1289466419932.jpg
62KB, 543x431px
>>60004208
local num = 0
num = 2
>>
>>60004521
Why do you hate fun? And this was something that still happened in the 80's.
>>
>>60004834
>fun
Only if you are some sort of nostalgic dinosaur I guess

>1980
That's about 15 years before I was even born
>>
>>60004283
GOT EM
>>
>>60004208
2
>>
3+3 without addition

flip symbols

- 3 - 3 = -6

flip result

6
>>
>>60005005
Flip off
>>
>>60004499
what cpu is that? lol
>>
File: cpu.jpg (207KB, 1036x924px) Image search: [Google]
cpu.jpg
207KB, 1036x924px
>>60005149
>>
>>60005168
is that the 6502?
>>
>>60005193
Greek ripoff
>>
>>60004251
>>60004347
>int only

Thanks, we'll call you back.
>>
>>60004563
if(a<b) {cout<<"First integer can't be smaller than the second. Please try again
"<<endl;}
>>
>>60005272
<<"Something happened."<<

Fixed that for you.
>>
for ( 0...b){
a++
}
>>
Easy as fuck:
Definition add : nat -> nat -> nat :=
let fix loop accu n {struct n} :=
match n with
| O => accu
| S na => loop (S accu) na
end in
fun a b => loop a b.

Eval compute in add 400 12.
>>
def add(a, b):
return a - (b * -1)
>>
>>60004208

a-(-b)
>>
int fround (float x) {
return (int) x + (x < 0.0 ? -0.5 : 0.5);
}

int add (int a, int b) {
return fround (log (exp (a) * exp (b)));
}
>>
>>60004234
uh, subtraction is really adding an inverse
>>
>>60004208
>>60005005
CL-USER> (defun addition (a b) (- (- (- a) b)))
ADDITION
CL-USER> (addition 5 5)
10
>>
>>60005272
Also can't be equal.
>>
    mov eax, 5
mov ebx, 7

; 5 + 7

neg ebx
sub eax, ebx

; or

lea eax, [eax+ebx]
>>
>>60005553
I don't know Coq, but isn't that just adding 1 to accu and subtracting 1 from b each loop? I didn't think that was allowed, but Coq seems pretty cool.
>>
>>60005859
or
    ; 5 + 7

mov eax, 5
mov ebx, 7
.l:
mov ecx, eax
and ecx, ebx
xor eax, ebx
mov ebx, ecx
shl ebx, 1
jnz .l

bitch
>>
>>60005898
You can do anything in Coq except writing bottom.
>>
>>60005949
translated to C for plebs that don't understand asm
int add(int a, int b) {
int c;
while (b) {
c = a & b;
a ^= b;
b = c << 1;
}
return a;
}
>>
>>60004641
Write a version that doesn't depend on endianness then.
>>
>>60005271
Floating point is trash.
>>
File: failure.png (93KB, 500x318px) Image search: [Google]
failure.png
93KB, 500x318px
>>60004234
It technically isn't adding either.
>>
>>60004208
Another homework thread? How do you expect to learn anything?
>>
>>60004764
kek
>>
>>60004541
take two 1-bit integers, then think about how you'd get their sum and carry to the 2nd-bit position from them, using only bit ops.

Then figure out how you handle the same idea, but with an extra carry input.

the sum goes into the 1-bit position, and the carry feeds into the next loop iteration

carry = 0 (initial carry is 0)
out0, carry = carry + a0 + b0
out1, carry = carry + a1 + b1
out2, carry = carry + a2 + b2
...

you get the idea
>>
>>60006228
it shouldn't depend on endianness because the data is all in registers
>>
>>60004208
Does this count?
function add(a, b)
if a > 0 then
return string.format("s(%s)", add(a - 1, b))
elseif b > 0 then
return string.format("s(%s)", add(a, b - 1))
else
return "0"
end
end

print(add(tonumber(arg[1]) or 0, tonumber(arg[2]) or 0))
>>
>>60004208
def halfadd(x,y):
return (x ^ y , (x & y) << 1)

def fulladd(x,y):
while y > 0:
(x,y) = halfadd(x,y)
return x
>>
>>60004208
add b = las t. take b . iterate succ
>>
function add(a, b)
int total = 0;
while (--a)
--total
while(--b)
--total
return -total
>>
>>60005950
>writing bottom
explain
>>
c = 0
while ((c - a - b) != 0):
c = random.random()*2**64 + random.random()
>>
a + b := (a^2 - b^2)/(a - b)
>>
>>60008409
Not him, but thanks for the explanation.
>>
Using bitshift and leading zeroes function:
add(x,y) := Lz((1 << a)*(1<<b))
>>
>>60005761
Addition is closed under the positive integers (and indeed any other positive set), subtraction is not. Different properties, therefore different functions.
>>
>>60004208
I won't do anything, faggot, you're just a spic.
>>
var s = new Stopwatch();
s.Start();
Thread.Sleep(a);
Thread.Sleep(b);
Console.WriteLine(s.ElapsedMilliseconds);
>>
>>60004234
>>60008715
Who mentioned positive integers? Why wouldn't all integers, real number, or even complex numbers be allowed?
>>
int addition (int a, int b)
{
int c;

while (a-c != b)
c = rand();

return c;
}
>>
Overflowing addition of 1-bit integers:
x & y
wrapping version:
x ^ y
>>
>>60004208
1 and 1 is 11

eat it faglord
>>
>>60008749
I used an example to demonstrate that addition and subtraction are not the same function.
>>
>>60008799
Sure, they're not the same function, but what the other anon said was correct. It's just adding an inverse.
If you consider that we're working with modular arithmetic, subtraction is still closed for the positive integers.
>>
>>60004482
You're hired!
>>
a-(!b)-1
>>
>>60008419
Write a version that doesn't depend on the existence of registers then.
>>
sum(5,4)
>>
>>60004208
here is a faster algorithm O(log_n) for bigger lengths

https://en.wikipedia.org/wiki/Kogge%E2%80%93Stone_adder
>>
>>60004893
>95
gtfo underageban
>>
>>60004208
while (b--)
a++;
>>
well allright 2 + 2 without addition is 2.....

+ FUCK FU CK GOD DAM CUNT YOU FUCKING FAGGOT I COULD HAVE DONE IT MY FINGER SLIPPED YOUF CUKING CUK CUKC UCKC OSIDJFOSIJFO*J$FO*JO*U(*!U(*U&(&(&&R
>>
>>60012068
in a post about addition
what a shit joke
>>
>>60004208
I'm a mathematician so I don't need a computer to add numbers
>>
File: tips-fedora-woman.jpg (72KB, 454x463px) Image search: [Google]
tips-fedora-woman.jpg
72KB, 454x463px
>>60004208
Maybe if you stopped flipping burgers, you'll learn how to flip bits.
>>
>>60012105
>without using addition
>uses addition and subtraction

Idiot.
>>
public static int add(int a, int b) {
if(a==0)
return b;
else if(b ==0)
return a;
else
throw new exception();
>>
File: 1492757876392.jpg (91KB, 614x673px) Image search: [Google]
1492757876392.jpg
91KB, 614x673px
(defun add (a b)
(when (= b 0) (return-from add a))
(add (logxor a b) (ash (logand a b) 1)))
>>
>>60005553
Definition := fix
Why not just Fixpoint add :=...?
You don't have to nest the fix inside a definition
>>
>>60005300
Go away Microsoft
>>
>>60004456
    int avg = floor(((float) a)/2 + ((float) b)/2);
>>
File: download copy.jpg (5KB, 132x128px) Image search: [Google]
download copy.jpg
5KB, 132x128px
<?php

add_wtht_addtn()
>>
Python:

a.__add__(b)

Easy
>>
>>60008733
nice c# stuff
>>
ADDC a, b
>>
>>60004208
>How about you add two numbers without using addition
Another one who has never heard of look up tables

 sum = table [a, b] 
Thread posts: 99
Thread images: 7


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.