IntExtension.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // IntExtension.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 12/08/14.
  6. // Copyright (C) 2014 Marcin Krzyżanowski <marcin.krzyzanowski@gmail.com>
  7. // This software is provided 'as-is', without any express or implied warranty.
  8. //
  9. // In no event will the authors be held liable for any damages arising from the use of this software.
  10. //
  11. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  12. //
  13. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  14. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  15. // - This notice may not be removed or altered from any source or binary distribution.
  16. #if os(Linux)
  17. import Glibc
  18. #else
  19. import Darwin
  20. #endif
  21. /* array of bits */
  22. extension Int {
  23. init(bits: [Bit]) {
  24. self.init(bitPattern: integerFromBitsArray(bits) as UInt)
  25. }
  26. }
  27. /* array of bytes */
  28. extension Int {
  29. /** Array of bytes with optional padding (little-endian) */
  30. public func bytes(totalBytes: Int = sizeof(Int)) -> [UInt8] {
  31. return arrayOfBytes(self, length: totalBytes)
  32. }
  33. public static func withBytes(bytes: ArraySlice<UInt8>) -> Int {
  34. return Int.withBytes(Array(bytes))
  35. }
  36. /** Int with array bytes (little-endian) */
  37. public static func withBytes(bytes: [UInt8]) -> Int {
  38. return integerWithBytes(bytes)
  39. }
  40. }
  41. /** Shift bits */
  42. extension Int {
  43. /** Shift bits to the left. All bits are shifted (including sign bit) */
  44. private mutating func shiftLeft(count: Int) -> Int {
  45. self = CryptoSwift.shiftLeft(self, count: count) //FIXME: count:
  46. return self
  47. }
  48. /** Shift bits to the right. All bits are shifted (including sign bit) */
  49. private mutating func shiftRight(count: Int) -> Int {
  50. if (self == 0) {
  51. return self;
  52. }
  53. let bitsCount = sizeofValue(self) * 8
  54. if (count >= bitsCount) {
  55. return 0
  56. }
  57. let maxBitsForValue = Int(floor(log2(Double(self)) + 1))
  58. let shiftCount = Swift.min(count, maxBitsForValue - 1)
  59. var shiftedValue:Int = 0;
  60. for bitIdx in 0..<bitsCount {
  61. // if bit is set then copy to result and shift left 1
  62. let bit = 1 << bitIdx
  63. if ((self & bit) == bit) {
  64. shiftedValue = shiftedValue | (bit >> shiftCount)
  65. }
  66. }
  67. self = Int(shiftedValue)
  68. return self
  69. }
  70. }
  71. // Left operator
  72. /** shift left and assign with bits truncation */
  73. public func &<<= (inout lhs: Int, rhs: Int) {
  74. lhs.shiftLeft(rhs)
  75. }
  76. /** shift left with bits truncation */
  77. public func &<< (lhs: Int, rhs: Int) -> Int {
  78. var l = lhs;
  79. l.shiftLeft(rhs)
  80. return l
  81. }
  82. // Right operator
  83. /** shift right and assign with bits truncation */
  84. func &>>= (inout lhs: Int, rhs: Int) {
  85. lhs.shiftRight(rhs)
  86. }
  87. /** shift right and assign with bits truncation */
  88. func &>> (lhs: Int, rhs: Int) -> Int {
  89. var l = lhs;
  90. l.shiftRight(rhs)
  91. return l
  92. }