Search code examples
swiftdarwin

How to call major or minor on dev_t using Swift


I obtained a value of type dev_t and I would like to access to the major and minor versions number of it, unfortunately while in C you would call major(theValue) or minor(theValue) they aren't defined in Swift (probably because they are just macros ?).

How should I do this ?

Thank you


Solution

  • dev_t is an signed 32-bit integer. major returns the most significant 8-bits and minor returns the least significant 24-bits.

    let x: dev_t = 0x12345678
    
    func major(_ x: dev_t) -> Int32 {
        return (x >> 24) & 0xff
    }
    
    func minor(_ x: dev_t) -> Int32 {
        return x & 0xffffff
    }
    
    print(String(major(x), radix: 16))
    print(String(minor(x), radix: 16))
    

    Output:

    12
    345678