Skip to content

Commit

Permalink
Refactored UInt32 and UInt64 methods nextPowerOf2 (#371)
Browse files Browse the repository at this point in the history
* * refactored UInt32 and UInt64 methods nextPowerOf2

* Added nextPowerOf2 to FixedWidthInteger
  • Loading branch information
adamnemecek authored and Lukasa committed May 1, 2018
1 parent a198092 commit a022954
Showing 1 changed file with 4 additions and 35 deletions.
39 changes: 4 additions & 35 deletions Sources/NIO/ByteBuffer-int.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,13 @@ extension ByteBuffer {
}
}

extension UInt64 {
extension FixedWidthInteger {
/// Returns the next power of two.
public func nextPowerOf2() -> UInt64 {
guard self > 0 else {
public func nextPowerOf2() -> Self {
guard self != 0 else {
return 1
}

var n = self

n -= 1
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n |= n >> 32
n += 1

return n
return 1 << (Self.bitWidth - (self - 1).leadingZeroBitCount)
}
}

Expand All @@ -137,25 +125,6 @@ extension UInt32 {

return n
}

/// Returns the next power of two.
public func nextPowerOf2() -> UInt32 {
guard self > 0 else {
return 1
}

var n = self

n -= 1
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n += 1

return n
}
}

/// Endianness refers to the sequential order in which bytes are arranged into larger numerical values when stored in
Expand Down

0 comments on commit a022954

Please # to comment.