[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]

hey faggots how wuold you switch the value of 2 variables with

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: 33
Thread images: 3

File: .jpg (19KB, 316x197px) Image search: [Google]
.jpg
19KB, 316x197px
hey faggots how wuold you switch the value of 2 variables with only one operation?
i have the answer, just curious of how you reflect on problems
>>
>>61243071
Install gentoo
>>
>>61243071
install ubuntu
>>
>>61243071
Do your own homework, fag.
>>
a=b=a;
>>
>>61243080
what a poorfag, ok i hope after this you will stop being butthole and starts talk serious.
when you have to swith to variables you have to declare a dummy variable right? wrong.
i tested this and it works and the program gains performances:
x=-x+(y=x)
your move now...
>>
>>61243112
>swith to variables
switch the value of two variables
>>
>>61243112
>y == x
>x == 0
Fucking idiot.
>>
>>61243124
lol what are you talkin about you learned yesterday to code
>>
Doesn't mean shit bcuz the uC will still use 2 instructions to change its registers values
>>
>>61243138
can you please try it before judging... it works.... i wanted only to spread knowledge
>>
File: 1484974434734.jpg (20KB, 500x281px) Image search: [Google]
1484974434734.jpg
20KB, 500x281px
x^=y^=x^=y;
>>
>>61243071
a = 42
b = 69
a = a^b
b = a^b
a = a^b
>>
>>61243130
$ cat test.c 
#include <stdio.h>

int main()
{
int x = 1, y = 2;

printf("%d %d\n", x, y);
x = -x + (y = x);
printf("%d %d\n", x, y);
}
$ gcc test.c
$ ./a.out
1 2
0 1
>>
>>61243166
it was x=y of course at the beginnig
>>
>>61243166
x=y-x+(y=x)
>>
>>61243112
the most arrogant are always the most ignorant.
that kind of code is incorrect because it can over/underflow.
your code is also identical to a naive version that use temporary storage and three instructions.
you also probably have never heard of the XCHG instruction.
most languages will also first evaluate y=x and return x as the result, so that you get x = -x + x, which means that x = 0 no matter what value x and y have.

>i tested this
haha no you have not

>>61243153
that doesn't require additional storage but is still three instructions.

>>61243160
still three instructions.
>>
x=c + (y=x)
where c is c(x,y) or c is a constant, it doesn't really matter
>>
a, b = b, a

https://en.wikipedia.org/wiki/Assignment_(computer_science)#Parallel_assignment
>>
>>61243071
Unless the ISA your using has an instruction for it, you can't.
>>
>>61243188
oh fuck I thought he wanted with no extra variables, welp.
>>
>>61243188
i misstyped it was x=y-x(y=x)
>>
>>61243185
That's undefined behaviour.
You have multiple uses of the same value (with side effects) that are sequenced relative to each other.
>>
>>61243188
y=x returns 1 as the result in the only good programming language, gtfo pajeet
>>
*you're
>>
swap(&a, &b);
>>
>>61243206
again, that is incorrect you dumb friend. What happens if y is the maximum positive integer and x is the minimum negative integer?
>>
>>61243207
>sequenced
unsequenced, I mean.
It's the same shit as
i = ++i;
>>
>>61243207
at the end of the equation x as the y value and y has the x value
>>
>>61243227
The program is undefined behaviour, therefore invalid.
Any and all output is meaningless.
>>
Either some C nested assignment magic, or many languages have memswap operation.
>>
>>61243071
xchg rax, rbx
>>
File: rekt.jpg (106KB, 611x438px) Image search: [Google]
rekt.jpg
106KB, 611x438px
>>61243222
>>61243222
i feel like you said things looking into a mirror.
pic related ahaha
Thread posts: 33
Thread images: 3


[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.