How to copy bits from M into N (numbers) between bit positions i and j (edge‑case‑safe) in Swift

1 Answer

0 votes
import Foundation

/*
    Handles edge cases:
    - Validates i and j
    - Prevents undefined behavior from shifting by ≥ 64 bits
    - Masks M so only the required bits are copied
*/

// Convert unsigned 64‑bit integer to binary string without leading zeros
func toBinary(_ x: UInt64) -> String {
    if x == 0 { return "0" }

    var result = ""
    var n = x

    while n > 0 {
        result.insert((n & 1 == 1) ? "1" : "0", at: result.startIndex)
        n >>= 1
    }

    return result
}

// Copy bits from M into N between bit positions [i, j]
func copyBits(_ N: UInt64, _ M: UInt64, _ i: Int, _ j: Int) -> UInt64 {
    if i < 0 || j < 0 || i > j || j >= 63 {
        fatalError("Invalid bit range")
    }

    let length = j - i + 1

    // Mask for bits i..j
    let mask: UInt64 = ((1 << length) - 1) << i

    // Clear bits i..j in N
    let Ncleared = N & ~mask

    // Take only the needed bits from M and shift into place
    let Mshifted = (M & UInt64((1 << length) - 1)) << i

    return Ncleared | Mshifted
}

/*
    N:      100 011111 00
    M:          110011
    Result: 100 110011 00
*/

let N: UInt64 = 0b10001111100
let M: UInt64 = 0b110011

let result = copyBits(N, M, 2, 7)

print("N:      \(toBinary(N))")
print("M:      \(toBinary(M))")
print("Result: \(toBinary(result))")



/*
OUTPUT:

N:      10001111100
M:      110011
Result: 10011001100

*/

 



answered Mar 31 by avibootz

Related questions

...