Skip to content

add and test bit_manipulation/swap_number.cpp - #3241

Open
ViCppDev wants to merge 3 commits into
TheAlgorithms:masterfrom
ViCppDev:master
Open

ViCppDev wants to merge 3 commits into
TheAlgorithms:masterfrom
ViCppDev:master

Conversation

@ViCppDev

@ViCppDev ViCppDev commented Oct 7, 2026

Copy link
Copy Markdown

Description of Change

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

i've added bit_manipulation/swap_numbers.cpp and tested it on both Debian v.13.7 and Arch Linux release 2026.10.1
it works with both clang++ and g++ ; it works in both debug (-g) and release (-O3) modes ; it works without any -W flags, and with -Wall -Wextra -Werror enabled

@realstealthninja realstealthninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to raise a pr!

Comment thread bit_manipulation/swap_numbers.cpp Outdated
Comment on lines +11 to +13
#include <cassert>
#include <cstdint>
#include <iostream>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Includes should be documented see other files for info

Comment thread bit_manipulation/swap_numbers.cpp
Comment thread bit_manipulation/swap_numbers.cpp Outdated
}
} // namespace bit_manipulation

inline static void test() noexcept {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test should not be inlined

Comment thread bit_manipulation/swap_numbers.cpp
Comment thread bit_manipulation/swap_numbers.cpp
@ViCppDev

ViCppDev commented Oct 7, 2026

Copy link
Copy Markdown
Author

i changed everything as you described

Comment thread bit_manipulation/swap_numbers.cpp
Comment thread bit_manipulation/swap_numbers.cpp Outdated
Comment on lines +31 to +35
* example:
* a = 7 = 0b111, b = 8 = 0b1000
* a ^= b; a == 0b1111 == 15
* b = a ^ b; b == 0b111 == 7
* a ^= b; a = 0b1000 == 8

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably add this to the top level documentation and convert it into a table/graphviz diagram and it would be much more legible

Comment thread bit_manipulation/swap_numbers.cpp
@ViCppDev

ViCppDev commented Oct 8, 2026

Copy link
Copy Markdown
Author

i added 5th test to actually cover every case (it won't hurt the performance if the values are equal, xor takes 3 bytes to encode on x86-64 machines, the jump instruction, e.g. the if statement will add more junk to the final code)
i tested it on both Debian and Arch linux, with both clang++ and g++

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants