IntExtension.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. import Foundation
  17. /* array of bits */
  18. extension Int {
  19. init(bits: [Bit]) {
  20. self.init(bitPattern: integerFromBitsArray(bits) as UInt)
  21. }
  22. }
  23. /* array of bytes */
  24. extension Int {
  25. /** Array of bytes with optional padding (little-endian) */
  26. public func bytes(totalBytes: Int = sizeof(Int)) -> [UInt8] {
  27. return arrayOfBytes(self, length: totalBytes)
  28. }
  29. public static func withBytes(bytes: ArraySlice<UInt8>) -> Int {
  30. return Int.withBytes(Array(bytes))
  31. }
  32. /** Int with array bytes (little-endian) */
  33. public static func withBytes(bytes: [UInt8]) -> Int {
  34. return integerWithBytes(bytes)
  35. }
  36. }
  37. /** Shift bits */
  38. extension Int {
  39. /** Shift bits to the left. All bits are shifted (including sign bit) */
  40. private mutating func shiftLeft(count: Int) -> Int {
  41. self = CryptoSwift.shiftLeft(self, count: count) //FIXME: count:
  42. return self
  43. }
  44. /** Shift bits to the right. All bits are shifted (including sign bit) */
  45. private mutating func shiftRight(count: Int) -> Int {
  46. if (self == 0) {
  47. return self;
  48. }
  49. let bitsCount = sizeofValue(self) * 8
  50. if (count >= bitsCount) {
  51. return 0
  52. }
  53. let maxBitsForValue = Int(floor(log2(Double(self)) + 1))
  54. let shiftCount = Swift.min(count, maxBitsForValue - 1)
  55. var shiftedValue:Int = 0;
  56. for bitIdx in 0..<bitsCount {
  57. // if bit is set then copy to result and shift left 1
  58. let bit = 1 << bitIdx
  59. if ((self & bit) == bit) {
  60. shiftedValue = shiftedValue | (bit >> shiftCount)
  61. }
  62. }
  63. self = Int(shiftedValue)
  64. return self
  65. }
  66. }
  67. // Left operator
  68. /** shift left and assign with bits truncation */
  69. public func &<<= (inout lhs: Int, rhs: Int) {
  70. lhs.shiftLeft(rhs)
  71. }
  72. /** shift left with bits truncation */
  73. public func &<< (lhs: Int, rhs: Int) -> Int {
  74. var l = lhs;
  75. l.shiftLeft(rhs)
  76. return l
  77. }
  78. // Right operator
  79. /** shift right and assign with bits truncation */
  80. func &>>= (inout lhs: Int, rhs: Int) {
  81. lhs.shiftRight(rhs)
  82. }
  83. /** shift right and assign with bits truncation */
  84. func &>> (lhs: Int, rhs: Int) -> Int {
  85. var l = lhs;
  86. l.shiftRight(rhs)
  87. return l
  88. }