Skip to content

Commit

Permalink
🚧 Unsigned bit mask
Browse files Browse the repository at this point in the history
  • Loading branch information
darthpelo committed Oct 1, 2017
1 parent 013abdc commit f78a100
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ xcuserdata

# Carthage
Carthage/Build
/TestPlayground.playground
4 changes: 4 additions & 0 deletions Bitter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
466264501C64AF01007B43AB /* Bitter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 466264451C64AF01007B43AB /* Bitter.framework */; };
466264551C64AF01007B43AB /* BitterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466264541C64AF01007B43AB /* BitterTests.swift */; };
466264621C64B036007B43AB /* Bitter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466264611C64B036007B43AB /* Bitter.swift */; };
D9038B091F7FA06D00DA257C /* BitterMaskTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9038B081F7FA06D00DA257C /* BitterMaskTest.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -32,6 +33,7 @@
466264561C64AF01007B43AB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
466264611C64B036007B43AB /* Bitter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bitter.swift; sourceTree = "<group>"; };
46E387061CC555CF00CCBA76 /* Templates */ = {isa = PBXFileReference; lastKnownFileType = folder; path = Templates; sourceTree = "<group>"; };
D9038B081F7FA06D00DA257C /* BitterMaskTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitterMaskTest.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -88,6 +90,7 @@
children = (
466264541C64AF01007B43AB /* BitterTests.swift */,
466264561C64AF01007B43AB /* Info.plist */,
D9038B081F7FA06D00DA257C /* BitterMaskTest.swift */,
);
name = BitterTests;
path = Tests/BitterTests;
Expand Down Expand Up @@ -212,6 +215,7 @@
buildActionMask = 2147483647;
files = (
466264551C64AF01007B43AB /* BitterTests.swift in Sources */,
D9038B091F7FA06D00DA257C /* BitterMaskTest.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
127 changes: 109 additions & 18 deletions Sources/Bitter/Bitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,21 @@ extension Int{
public var toInt:Int{return self}
/// Perform a bit pattern truncating conversion to UInt
public var toUInt:UInt{return UInt(bitPattern:self)}

/// Returns a Int with all ones
public static var allOnes:Int{return Int(bitPattern: UInt.max)}

public func mask(bits: Int, msb: Bool) -> Int {
var maskBits = bits
let size = Int.size
if msb {
for i in 0...((size/2) - 1) { maskBits[i] |= 0xFF }
return maskBits.toInt
} else {
for i in ((size/2))...(size - 1) { maskBits[i] |= 0xFF }
return maskBits.toInt
}
}
/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<Int>.stride}

Expand Down Expand Up @@ -120,7 +131,7 @@ extension Int{
let nv: UInt = bit != 0 ? 1 : 0
return Int(bitPattern: ( UInt(bitPattern: self) & ~(0x1 << 7)) | (nv << 7) )
}


/// Subscript that returns or set one of the bytes of a Int
/// at the given index.
Expand Down Expand Up @@ -170,10 +181,26 @@ extension UInt{
public var toInt:Int{return Int(bitPattern:self)}
/// Perform a bit pattern truncating conversion to UInt
public var toUInt:UInt{return self}

/// Returns a UInt with all ones
public static var allOnes:UInt{return UInt.max}

public func mask(bits: UInt, msb: Bool) -> UInt {
var maskBits = bits
let size = UInt.size
let halfSize = size / 2
if msb {
for i in 0...(halfSize - 1) {
maskBits[i] |= 0xFF
}
return maskBits.toUInt
} else {
for i in (halfSize)...(size - 1) {
maskBits[i] |= 0xFF
}
return maskBits.toUInt
}
}
/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<UInt>.stride}

Expand Down Expand Up @@ -250,7 +277,7 @@ extension UInt{
let nv:UInt = bit != 0 ? 1 : 0
return ( self & ~(0x1 << 7)) | (nv << 7)
}


/// Subscript that returns or set one of the bytes of a UInt
/// at the given index.
Expand Down Expand Up @@ -300,6 +327,8 @@ extension Int8 {
/// Returns a Int8 with all ones
public static var allOnes:Int8{return Int8(bitPattern: UInt8.max)}

public static var mask:Int8{return Int8(bitPattern: UInt8.max)}

/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<Int8>.stride}

Expand Down Expand Up @@ -375,7 +404,7 @@ extension Int8 {
let nv = bit != 0 ? 1 : 0
return ( (self.toU8 & ~(0x1 << 7)) | (nv.toU8 << 7) ).to8
}



/// Subscript that returns or set one of the bytes of this integer
Expand Down Expand Up @@ -424,6 +453,18 @@ extension UInt8 {
/// Returns a UInt8 with all ones
public static var allOnes:UInt8{return UInt8.max}

public func mask(bits: UInt8, msb: Bool) -> UInt8 {
var maskBits = bits

if msb {
maskBits |= 0x0F
return maskBits
} else {
maskBits |= 0xF0
return maskBits
}
}

/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<UInt8>.stride}

Expand Down Expand Up @@ -499,7 +540,7 @@ extension UInt8 {
let nv = bit != 0 ? 1 : 0
return (self & ~(0x1 << 7)) | (nv.toU8 << 7)
}



/// Subscript that returns or set one of the bytes of this integer
Expand Down Expand Up @@ -548,6 +589,8 @@ extension Int16 {
/// Returns a Int16 with all ones
public static var allOnes:Int16{return Int16(bitPattern: UInt16.max)}

public static var mask:Int16{return Int16(bitPattern: UInt16.max)}

/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<Int16>.stride}

Expand Down Expand Up @@ -623,10 +666,10 @@ extension Int16 {
let nv = bit != 0 ? 1 : 0
return ( (self.toU16 & ~(0x1 << 7)) | (nv.toU16 << 7) ).to16
}



/// Subscript that returns or set one of the bytes of a Int16

/// Subscript that returns or set one of the bytes of a Int16
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> Int16 {
Expand Down Expand Up @@ -672,6 +715,18 @@ extension UInt16 {
/// Returns a UInt16 with all ones
public static var allOnes:UInt16{return UInt16.max}

public func mask(bits: UInt16, msb: Bool) -> UInt16 {
var maskBits = bits

if msb {
maskBits[0] |= 0xFF
return maskBits
} else {
maskBits[1] |= 0xFF
return maskBits
}
}

/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<UInt16>.stride}

Expand Down Expand Up @@ -747,10 +802,10 @@ extension UInt16 {
let nv = bit != 0 ? 1 : 0
return (self & ~(0x1 << 7)) | (nv.toU16 << 7)
}



/// Subscript that returns or set one of the bytes of a UInt16

/// Subscript that returns or set one of the bytes of a UInt16
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> UInt16 {
Expand Down Expand Up @@ -796,6 +851,8 @@ extension Int32 {
/// Returns a Int32 with all ones
public static var allOnes:Int32{return Int32(bitPattern: UInt32.max)}

public static var mask:Int32{return Int32(bitPattern: UInt32.max)}

/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<Int32>.stride}

Expand Down Expand Up @@ -871,10 +928,10 @@ extension Int32 {
let nv = bit != 0 ? 1 : 0
return ( (self.toU32 & ~(0x1 << 7)) | (nv.toU32 << 7) ).to32
}



/// Subscript that returns or set one of the bytes of a Int32

/// Subscript that returns or set one of the bytes of a Int32
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> Int32 {
Expand Down Expand Up @@ -920,6 +977,22 @@ extension UInt32 {
/// Returns a UInt32 with all ones
public static var allOnes:UInt32{return UInt32.max}

public func mask(bits: UInt32, msb: Bool) -> UInt32 {
var maskBits = bits

if msb {
let size = UInt32.size
let halfSize = size/2
for i in 0...(halfSize - 1) { maskBits[i] |= 0xFF }
return maskBits
} else {
let size = UInt32.size
let halfSize = size/2
for i in (halfSize)...(size - 1) { maskBits[i] |= 0xFF }
return maskBits
}
}

/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<UInt32>.stride}

Expand Down Expand Up @@ -995,10 +1068,10 @@ extension UInt32 {
let nv = bit != 0 ? 1 : 0
return (self & ~(0x1 << 7)) | (nv.toU32 << 7)
}



/// Subscript that returns or set one of the bytes of a UInt32

/// Subscript that returns or set one of the bytes of a UInt32
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> UInt32 {
Expand Down Expand Up @@ -1044,6 +1117,8 @@ extension Int64 {
/// Returns a Int64 with all ones
public static var allOnes:Int64{return Int64(bitPattern: UInt64.max)}

public static var mask:Int64{return Int64(bitPattern: UInt64.max)}

/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<Int64>.stride}

Expand Down Expand Up @@ -1119,10 +1194,10 @@ extension Int64 {
let nv = bit != 0 ? 1 : 0
return ( (self.toU64 & ~(0x1 << 7)) | (nv.toU64 << 7) ).to64
}



/// Subscript that returns or set one of the bytes of a Int64

/// Subscript that returns or set one of the bytes of a Int64
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> Int64 {
Expand Down Expand Up @@ -1168,6 +1243,22 @@ extension UInt64 {
/// Returns a UInt64 with all ones
public static var allOnes:UInt64{return UInt64.max}

public func mask(bits: UInt64, msb: Bool) -> UInt64 {
var maskBits = bits

if msb {
let size = UInt64.size
let halfSize = size/2
for i in 0...(halfSize - 1) { maskBits[i] |= 0xFF }
return maskBits
} else {
let size = UInt64.size
let halfSize = size/2
for i in (halfSize)...(size - 1) { maskBits[i] |= 0xFF }
return maskBits
}
}

/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<UInt64>.stride}

Expand Down Expand Up @@ -1243,10 +1334,10 @@ extension UInt64 {
let nv = bit != 0 ? 1 : 0
return (self & ~(0x1 << 7)) | (nv.toU64 << 7)
}



/// Subscript that returns or set one of the bytes of a UInt64

/// Subscript that returns or set one of the bytes of a UInt64
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> UInt64 {
Expand Down
Loading

0 comments on commit f78a100

Please sign in to comment.