""""""Simultaneous""""""" incrementation

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include <stdint.h> #include <stdbool.h> typedef uint32_t u32; typedef uint64_t u64; int main() { u32 nums[64]; for (int i = 0; i < 64; ++i) nums[i] = 0; int n = 0; while (scanf("%u", &nums[n]) > 0) ++n; // Each element stores one bit from each number, from the same position. // At index 0 - least significant bits, index 32 - most significant bits. u64 bit_columns[32]; for (int bit_i = 0; bit_i < 32; ++bit_i) { bit_columns[bit_i] = 0; for (int num_i = 0; num_i < 64; ++num_i) { bool is_on = (nums[num_i] & (1 << bit_i)) != 0; if (is_on) { bit_columns[bit_i] |= 1 << num_i; } } } u64 carry = -1; for (int bit_i = 0; bit_i < 32; ++bit_i) { // If we still carry a bit or just begun and the current bit in the number is one, // set it to zero, otherwise set it to one (XOR). bit_columns[bit_i] ^= carry; // If we did set it to one and we have a carry bit set to one, set the carry to zero. carry &= ~(carry & bit_columns[bit_i]); } for (int num_i = 0; num_i < n; ++num_i) { u32 num = 0; for (int bit_i = 0; bit_i < 32; ++bit_i) { bool is_on = (bit_columns[bit_i] & (1 << num_i)) != 0; if (is_on) num |= 1 << bit_i; } printf("%u\n", num); } return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines