#include <Arduino.h>

// Define a structure to represent a 256-bit number using an array of 32 uint8_t elements
typedef struct {
    uint8_t parts[32];
} uint256_t;

// Function to print the 256-bit number in hexadecimal format
void printNumber(const uint256_t &num) {
    for (int i = 31; i >= 0; i--) {
        if (num.parts[i] < 0x10) {
            Serial.print("0");  // Print leading zero for single digit values
        }
        Serial.print(num.parts[i], HEX);
        if (i > 0) Serial.print(" ");
    }
    Serial.println();
}

// Function to calculate the two's complement of a 256-bit number
void twosComplement(uint256_t &num) {
    // Step 1: Invert all bits
    for (int i = 0; i < 32; i++) {
        num.parts[i] = ~num.parts[i];
    }

    // Step 2: Add 1 to the least significant byte and handle carry
    uint16_t carry = 1;  // Start with adding 1
    for (int i = 0; i < 32; i++) {
        uint16_t sum = num.parts[i] + carry;
        num.parts[i] = sum & 0xFF;  // Keep only the lower 8 bits
        carry = (sum >> 8) & 0xFF;  // Carry over the upper bits
    }
}

void setup() {
    Serial.begin(9600);

    // Initialize a 256-bit number (for example: 0x00000000000000000000000000000001)
    uint256_t myNumber = {0};
    myNumber.parts[0] = 1;

    // Print original number
    Serial.print("Original number: ");
    printNumber(myNumber);

    // Get the two's complement
    twosComplement(myNumber);

    // Print the two's complement
    Serial.print("Two's complement: ");
    printNumber(myNumber);
}

void loop() {
    // Your code here
}