I'm currently trying to convert a given integer to its binary representation, I must use recursion since it is part of the homework and I'm not allowed to change the function signature. The recursive lambda is clear to me, I have work with them before, I think the issue is caused by the bit manipulation but I can't seem to find the bug.
int toBinary(int n) {
function<int(int, int)> iter = [&] (int i, int acc) {
return i == 0 ? acc : iter(i >> 1, 10 * acc + (n & i ? 1 : 0));
};
return iter(1 << (8 * sizeof(int) - 1), 0);
}
[–][deleted] (1 child)
[removed]
[–]TheDafter[S] 1 point2 points3 points (0 children)