How do you average two integers in C?

How do you average two integers in C?

Attached: 615e912d557197d688f01d65bcba69c8.jpg (667x1050, 91K)

add them together and divide by 2

t. Pajeet

divide them by 2 and add them together. if they are both odd, add 1 to the final result

#include

int main(void)
{
float a, b, avg;
avg = (a + b)/2;
return 0;
}

Wrong

segfaulted for me

i would average her out if you know what i mean.

I get it that you typically need node.js to average two integers OP
but you could also do the following
unsigned int result = (unsigned int) ( integerA + integerB ) / 2;

Learn to math.

a * 0.5 + b * 0.5

Less efficient than adding and then dividing

If (a+b)/2 == (1+a+b)2 the answer is (a+b)/2, otherwise it is (a+b)/2 + 1/2.

Adding before dividing might overflow

He's not wrong. You divide by 2.0

But also less likely to overflow.

adding then dividing can introduce integer overflow. The correct solution is

What happens if one of the numbers is INT_MAX?

Here’s JS
(a+b)/2
how will C-ucks recover?

Well, it depends on what your use case is then...
For an HPC application where you have a good idea of the magnitude of those numbers, you'll probably take the faster version anyway.

>how will C-ucks recover?
by having their code run in 3 cpu cycles instead of 3000 cycles

Overflow. Just use exceptions :^)

Attached: javascript.png (1452x2208, 843K)

Last I checked C doesn't have exception handling though?

OP clearly made this thread to weed out anyone who isn't thinking about overflows. Just like that buffer overflow thread.

Attached: for-the-retards.png (853x960, 793K)

>No tits
What is the point? It would be like fucking a man.

Add them manually, of course, using IF statements.

Sup boomer.

Small tits are cute

with an if statement if bigger than X round also do the same for negatives

What in the heavenly name of fuck gave you the flaming urge to put a question mark?

I've seen wooden planks less flat, lol.

avg = a + abs(b-a)/2

Jealous roastie

gcc -ftrapv

a + b >> 1

>user's preferred female body

Attached: non-dfc-1.jpg (500x322, 70K)

>average of 20 and 10 is 25

Attached: wat-15.jpg (337x290, 56K)

>he thinks div will be performed in 1 cycle

hmmmmmmmmmm maybe she has a vagina? or maybe I want to see her gook eyes roll into the back of her head

user please read

r u retared? just do (b+a)/2

Now that's some quality udders right there.

>js tard doens't know what bit shifting is
It's okay to be retarded user. I feel for you

>what is integer overflow

just dont overflow it lol

ah, I hadn't considered the compiler realising it's division by a power of 2. fair snuff.
seriously tho, projecting much?

avg = min(a,b) + abs(b-a)/2

easily avoidable when you have bigger integer types

fuck off pajeet, we don't get paid based on how much cpu we waste (otherwise we'd be writing JS)

Stop watching porn and you can figure it out your self. Nothing hurts the intellect like sexual habit taken to it's logical extreme.

#define shift(x) (x)
usigned long long a,b;
cout ((a&1)&&(b&1)) + shift(a) + shift(b);

what do you even mean? why would you need a bigger integer type here? wouldn't you do some kind of conversion to float ?

>wants an average in C
>posts two As
never gonna make it

C has no native big integer types

>the average of 2 and 2 is 3

Source?

nevermind, read your post wrong

(max(x,y)-min(x,y))/2+min(x,y)
If you want to preserve int.

Stop watching porn. It never satiates. You're doing the same thing over and over again.

>It would be like fucking a man.
hot

>Sleeping, eating
>Doing it every day
>Never satiates
>This is somehow bad