Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactored UInt32 and UInt64 methods nextPowerOf2 #371

Merged
merged 4 commits into from
May 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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