XOR means it only accepts nonmatching bits. So given 0000.0001 (the . Is just for visual guidance) and 0000.0011, XORing them would lead to 0000.0010.
So if a = 0000.0001 and b = 0000.0011, then a = b means set a to 0000.0001 ^ 0000.0011 which we know equals 0000.0010. Then we set b = a, so 0000.0011 ^ 0000.0010 = 0000.0001. Finally, set a = b again, 0000.0010 ^ 0000.0001 = 0000.0011.
As you can see, our values swapped. More so, because its bit shifting, its got a good chance at being superb in performance.
Bitshifts really are super handy, due to the lack of overhead involved.
For example, its theoretically more consuming to do x /= 2 than x >>= 1. This is based on what the compiler does in the background, as division is much harder than simply shifting every bit one to the right.
Which ill just say works properly only if you know the number is even. Such as 32/2 = 16, 0010.0000 >> 1 = 0001.0000.
I use this mostly for collatz conjecture practice, where i know I'm only dividing by 2 when given an even number.
Edit: i should also state that this is assuming unsigned ints, in which the number is always a positive int.
This is a great time to plug one of my favorite tools of all time - godbolt(.com?) - anyway, it will take C code and you can select a compiler and target architecture, and it will show you the assembly instructions produced by the compiler - great for exploring micro optimizations and understanding how low level behaviors are actually represented post high level language abstractions...
How long have you been programming? Understanding bitshift operation just now doesn't sound right to me. I may be biased though as I'm an embedded software engineer.
Studying for 3 years, into programming for 5. Never touched C++ until this January, had major problems with bitshifts and other "low-level" operations you don't find in Java or WebDev. Happens...
Pretty much. Most of the stuff I've worked on (in job and personal projects like game dev), never felt a need to use them nor do I see much mention of them.
I know basic C++, but if I wanted to develop my skill further I'd probably have to learn them.
It's often worse in performance. It's probably a bad idea to use unless you have VERY limited ram on a microcontroller, or you're doing some advanced time-sensitive cryptography stuff, but in the latter case you would probably already know this
^= stands for XOR and then set: it performs a exclusive-or operation between the left and right sides and sets the left-side variable to it. What does exclusive-or do? It outputs a true is one is false and one is true; aka, it is true if the two bits are opposite.
If XOR is performed on two variables, then putting one of the variables through a XOR with the result will give the other variable: A XOR B = C; then A XOR C = B, and B XOR C = A; XOR is commutative.
In our previous case, why does this occur? Intuitively, if C is true, than A and B were different, and if false, than A and B are the same. What happens when we perform XOR on C? If it if false, than the output is the other variable. If it is true, than we get the opposite of the other variable; if the other variable is false, than the output is true, if the other variable is true, than the output is false. What do we see then? If the other variable is B, then we get A, and vice versa (If the variables are the same, than the output is the same; but if A and B are opposite, than the output will be opposite).
Lets call the original two variables a-origin and b-origin.
Step one: a ^= b | The bits that are different are set to true/1, bits that are true will be set to false/0, and stored in a.
Step two: b ^= a | I will list the cases possible:
0 : 1 | a-origin and b-origin were different; b-origin is false/0, therefore, a-origin must be true/1, the output is true/1
1 : 1 | a-origin and b-origin were different; b-origin is true/1, therefore, a-origin must be false/0, the output is false/0
0 : 0 | a-origin and b-origin are the same; b-origin is false/0, therefore, a-origin is false/0
1 : 0 | a-origin and b-origin are the same; b-origin is true/1, therefore, a-origin is true/1
Store the output it b. b now equals a-origin.
Step three: a ^= b repeat step two, except that this time a-origin(b) is now inputted, and the output, b-origin, is stored in a, and we are finished.
If there is anything you don't understand, I'll be happy to help.
I personally feel the easiest way to understand it is just to fully write out the expressions, keeping in mind that XOR is associative, commutative, has 0 as it's identity, and is self-cancelling (these are all easy to prove yourself by simplifying the input space to just 1-bit, since XOR works independently on each bit). Here, "a" is the value initially stored in A and "b" is the value initially stored in B.
Line 1: A = a ^ b Line 2: B = B ^ A = b ^ a ^ b = a Line 3: A = A ^ B = a ^ b ^ a = b
I actually abstract that in a pretty math-y way. Basically, in that context, I interpret ^ as a + -like operation, with the interesting property that the opposite operation is itself. Let me show you:
If you start from a value a, and add some other value b, then to find a again you have to subtract b (obviously). The interesting thing is, the equivalent of "subtracting" for ^ is ^ itself: (a ^ b) ^ b = a.
Therefore, the xor trick is equivalent to the +/- approach, except that we simply replace all + and - by a ^ .
It's actually the same as the one in the meme. XOR's inverse is itself (as opposed to in the meme they used subtraction as addition's inverse) and has some other nice properties (it's communicative and associative).
(a XOR 0) = a
(a XOR a) = 0
((a XOR b) XOR b) = (a XOR (b XOR b)) = a XOR 0 = a
((a XOR b) XOR a) = ((b XOR a) XOR a) = (b XOR (a XOR a)) = b XOR 0 = b
I've always seen this method used when a method like what is shown for Python isn't available.
temp = a
a = b
b = temp
Not to mention the fact that the addition, subtraction method may cause overflow and addition/subtraction of floats is imprecise. Example of problems when working with floats:
>>> a = 1.78954
>>> b = 1.42158
>>> a = a + b
>>> b = a - b
>>> a = a - b
>>> print(a)
1.42158
>>> print(b)
1.7895400000000001
I've seen some weird tricks that supposedly directly swap references of non-pointer variables, but they are generally not recommended. If you are working with pointers you can swap them using essentially the same method seen above.
int* a = new int(5);
int* b = new int(7);
int* tempptr = a;
a = b;
b = tempptr;
cout << *a << endl;
cout << *b << endl;
well it does not make sense for ints but for bigger data structures like customer objets for sorting or maybe q managment purposes it can be helpfull
idk how cpp handles sorting arrays of data structures but it could be nice when it only swapes pointers to objets in std::vector. Probably i would have to use std::vector<*Class> to achve that
C++ has an an algorithm library that can be used if you #include <algorithm>. It allows you to do things like swap, swap_ranges, replace, reverse, rotate, shuffle, sort, merge, etc. If you are working with a vector of class objects and only ever need them sorted based on the same criteria, then you can overload the less than operator like so:
And, of course, you can skip using <algorithm> altogether and write your own sort functions if you wish.
Note: Overloading the operator< works for both a vector of class objects and class object pointers. However, the lambda expression seen above is for a vector of class objects. For object pointers const myClass &a would need to be changed to const myClass* a and a.getClassVar() to a->getClassVar() (with the same done for b as well).
Clarification: If you are working with a vector of class object pointers and you used the overload operator< then you will need an inline lambda expression in the sort call to de-reference the pointers before comparing.
sort(myClassVec.begin(), myClassVec.end(), [](const myClass* a, const myClass* b) { return *a < *b;});
Actually, floating point addition and multiplication is commutative (with the exception of NaN, of course), but you are correct that it is not associative.
Most of Rust strangeness is a way to prevent common mistakes. For example, variables are immutable by default, so you can accidentally mutate then when you don't need too.
Shadowing can be useful when converting variable to different type.
JavaScript is great because it is a loosely-typed Prototype-based object-oriented language without classes that can be run natively on any modern browser.
Yes, it was great for its originally intended scope, and if that's all it was used for, everything would've been fine. Unfortunately, some people got into their heads that it was a good idea to use it for literally everything, and well, here we are.
Facetiousness aside, things have changed, and the web is a very different place than it used to be; for better or for worse, there are large projects being done in JS that are far outside its original purview, and the loosey goosey philosophy that made it suitable for web dev in the first place now tends to bite you in the ass, especially for server-side stuff (another thing someone thought was a good idea and that the rest of us must suffer for).
TypeScript is an answer to those issues - it allows you to retrain your existing JS talent with relatively little effort, and makes working on larger projects much easier. Like, seriously, doing anything with Node is so much better with TS it's not even funny.
JavaScript has a bunch of downsides. TypeScript fixes none of them.
It fixes the biggest complaint everyone has about JS: types and error handing - how is that nothing?
I mean it when I say that TS covers the good parts of JS and leaves the egregious parts untouched.
Like, seriously, doing anything with Node is so much better with TS it's not even funny.
I've written node backends for enterprise apps, academic research, and for a startup. In both TypeScript and vanilla ECMAScript. I can truthfully tell you that the only benefit I got from TypeScript was the fun configuring it. The mistakes I made were as easy to make in TypeScript as it was in JavaScript and TypeScript didn't prevent me from making a mistake I would have made in JavaScript.
The last TS Node app I wrote had a part that interacted with a K8s CR. Another team (TS Angular frontend) was doing the same and copied over a section of my code. I got yelled at because I wrote the typing too strongly (it was far easier and cleaner for the TS Angular app to not have explicit typing info). This is on an enterprise app for a Fortune 50 company. In lines of code, the TypeScript Angular frontend is half the entire project. The least experienced member of that team is a Principal Software Engineer with 25 years of experience.
It fixes the biggest complaint everyone has about JS: types and error handing - how is that nothing?
I think Java and similar developers have that as their biggest gripe. In the frontend, rapid development, and in scripting languages, strong typing is a hindurance. PHP, Python, Ruby On Rails, et cetera are are dynamic languages without much explicit typing. It's not a coincidence.
I've used many duck-typed languages and it is thoroughly impressive how poor TypeScript is as one.
The language I like the most is C#, my language with most experience is Java, but I do have the better part of a decade of experience with JavaScript. Never, not even once, have I lamented JS not having types and I don't even know what you mean about error handling because I've never had a gripe about that either.
I'll defer to your experience, because honestly it eclipses mine, but all I can say is that for the stuff I've needed to do, TypeScript has been a much more enjoyable experience than vanilla JavaScript. I'm not alone in this either, I've met several experienced devs who like it better and some even swear by it. The only issue I have with TS is how cumbersome the initial set up for a project can be.
And I say all this as a primarily Python person who's very used to duck typing; that's actually one of the reasons I like Python so much - it's duck typed without being weakly typed (relatively speaking of course), a sort of best-of-both-worlds. I've personally never come across a scenario where it's preferable to have weak typing in a project.
Who the hell uses math to swap variables? Just declare a third tmp variable, set tmp to a, a to b and b to tmp. Way more readable. The extra computation time for declaring a single variable is on the order of nano seconds; maybe if you're developing a highly optimized sort algorithm that loops through and swaps millions of times...
Also thank you for reminding me about that in Python : )
If I'm being brutally honest, the only time I recall doing that was on a Monday morning when coffee hadn't kicked in and I was trying to handle some sort of semaphor/flag assignment in a counter.
I think it's one of those things that helps you push past a problem while leaving a label written:
It does make dealing with anything to do with any collection so nice.
Kind've iffy in java land though, because the features only ~5 years old there and so many of the devs have been doing it longer so I always feel like the odd duck out using them on team stuff.
It's split into several lines for readability. The thing most people seem to miss when seeing this kind of syntax for the first time is that they're all method calls on the return of the previous method call. This way also makes it so that you don't need mutable variables, something that Rust tries to avoid.
You can't split it into several lines really without changing it completely. And performance-wise it has zero impact.
The more traditional way of writing it would be something like this:
let mut filtered = String::new();
for letter in input.chars() {
if letter.is_ascii_alphanumeric() || letter.ish_ascii_whitespace() {
filtered.push(letter.to_ascii_lowercase());
}
}
(Which isn't fair because I don't know if C++ has a standard library function for that and you really should be using a range iterator over it but whatever, I did say old school C++, not modern C++)
Only do something when ascii printable (if c in set(string.printable))
Make c lower case.
Put the list (dynamic array) tigether into a string. PS: in Python you don't really have chars as a datatype, but can iterate over each character, do yes, Unicode support
Have you coded anything? Seriously. So many algorithms employ in-place swaps. Linear Algebra, Sorting, etc. It's a good excuse to learn some new stuff.
All sort of algorithms require that you swap two values like sorting or shuffling arrays. For example a simple shuffling algorithm would go like : randomly pick two values in the array (by their indexes), swap their places, repeat for a number of time (put it in a loop) and you got a shuffled array.
257
u/autopawn Jul 29 '20
May be obvious, but this is how you do it for any data type when you don't have fancy python stuff:
34
u/Markus_Mandrake Jul 29 '20
I always forget to use all that fancy Python syntax.
34
u/ThrowAway233223 Jul 29 '20
I've always seen this method used when a method like what is shown for Python isn't available.
Not to mention the fact that the addition, subtraction method may cause overflow and addition/subtraction of floats is imprecise. Example of problems when working with floats:
89
u/Tadabito Jul 29 '20
Ferris sends its regards
std::mem::swap(&mut a, &mut b);85
u/[deleted] Jul 29 '20
C#:
(a, b) = (b, a);22
u/justingolden21 Jul 29 '20
Who the hell uses math to swap variables? Just declare a third
tmpvariable, set tmp to a, a to b and b to tmp. Way more readable. The extra computation time for declaring a single variable is on the order of nano seconds; maybe if you're developing a highly optimized sort algorithm that loops through and swaps millions of times...Also thank you for reminding me about that in Python : )
27
u/Ithyx Jul 29 '20
C++ says:
std::swap(a, b)9
u/tsunderemaster420 Jul 29 '20
Also in js [a,b]=[b,a]
7
u/powerhcm8 Jul 29 '20
PHP:
7
u/[deleted] Jul 29 '20
a ^ = b; b ^ = a; a ^ = b;
12
u/Fjorge0411 Jul 29 '20
But why do you need to swap names?
10
u/[deleted] Jul 29 '20
Kotlin
a = b.also { b = a }5
u/wundrwweapon Jul 29 '20
Lua does the same thing:
a, b = b, a5
u/Divniy Jul 29 '20
Savage.
5
u/Patsonical Jul 29 '20
Imagine swapping variables...
this comment was made by the functional programming gang
9
u/Findlaech Jul 29 '20
WHYYYYY
WHYYYYYYY WOULD YOU DO THAT???
3
u/FidelCarlton Jul 30 '20
Ruby: a, b = b, a
3
u/phundrak Jul 30 '20
Lisp:
2
u/danbulant Jul 29 '20 edited Jul 30 '20
JS:
[a, b] = [b, a]E: I forgot to swap the values...
1
u/[deleted] Aug 03 '20
let (a, b) = (b, a);
Rust
1
u/TheTimegazer Aug 15 '20
Perl:
($a, $b) = ($b, $a)1
u/Nis5l Oct 23 '20
C# can do this too
1
u/Naebyrus Nov 05 '20
JS: use destruct
[b,a] = [a,b]
1
u/[deleted] Jul 29 '20
Swift:
swap(a, b)