4 ms·
Why is it called "byteswap" and not "bytereverse"? "Swapping" can be done in any number of ways, but only one way to reverse.
by XCSme 4y ago
Why is it called "byteswap" and not "bytereverse"? "Swapping" can be done in any number of ways, but only one way to reverse.
- deleted 4y ago[deleted]
- siknad 4y agoReversing bytes sounds like reversing their bit orders to me.
- fluoridation 4y agoCould be called "byte-wise reverse".
- XCSme 4y agoThat would be "bitreverse" I guess, plus their description of byteswap[0] is exactly that: "Reverses the bytes in the given integer value n." I would expect the description for "byteswap" to be something like "swaps the given bytes of an integer value n." and be called like byteswap(x, a, b), where a and b are the indicies of the bytes to be swapped. [0]: https://en.cppreference.com/w/cpp/numeric/byteswap https://en.cppreference.com/w/cpp/numeric/byteswap
- secondcoming 4y agoThere's an x64 instruction called BSWAP.
- pm215 4y agoI don't know, but 'swap' seems to have become the standard term for the operation, used in places like the C bswap family of functions and the x86 'bswap' instruction. My (totally unsupported and unresearched) guess is that it became popular as a term when 16-bit architectures were common -- "swap the bytes in a 16 bit value" is unambiguous. The Arm architecture does call this operation "reverse bytes", though, so it's not universal to call it "swap".
- XCSme 4y agoThat makes sense.
- Koshkin 4y agoBecause the reversal is done by swapping the first byte with the last, etc.
- XCSme 4y agoBut on the same note, sorting is also done by swapping elements.
- Koshkin 4y agoNot if you look at the result.
- XCSme 4y agoWhat do you mean? Sorting is done by swapping elements ( swap(a[i], a[j]) )
- Koshkin 4y agoWell, first, whether swap() is used or not depends on the algorithm; second, the result of the sorting, as opposed to reversal, does not necessary look like the elements were swapped.
- XCSme 4y agoOh, based on your other comment, I finally understood what you meant initially. You were saying that the final array doesn't have many indices i,j such that a[i] = sorted_a[j] and a[j] = sorted_a[i]. My initial comment was not referring to the final order of the array, but the operations made to reach that order (one or multiple swaps). Another example could have been saying that we could have named the "sort" method "compare" because it uses comparisons in its algorithm (which was a parallel to your initial comment that it's called "swap" because the reverse operation uses swaps to achieve this).
- XCSme 4y ago> whether swap() is used or not depends on the algorithm I didn't mean the std::swap() function itself, but the swapping of two elements a[i] and a[j]. Is there any sorting algorithm that doesn't rely on swapping two elements (or two parts of the array)? I guess, only if the sort is not being done in-place and the result is stored in a different variable/memory. > does not necessary look like the elements were swapped The only way elements don't look like they are swapped is elements are changed, added or removed, which doesn't happen during sort.