Int+Extension.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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: integerFrom(bits) as UInt)
  25. }
  26. }
  27. /* array of bytes */
  28. extension Int {
  29. /** Int with collection of bytes (little-endian) */
  30. init<T: Collection>(bytes: T) where T.Iterator.Element == UInt8, T.Index == Int {
  31. self = bytes.toInteger()
  32. }
  33. /** Array of bytes with optional padding (little-endian) */
  34. func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
  35. return arrayOfBytes(value: self, length: totalBytes)
  36. }
  37. }
  38. /** Shift bits */
  39. extension Int {
  40. /** Shift bits to the left. All bits are shifted (including sign bit) */
  41. mutating func shiftLeft(by count: Int) {
  42. self = CryptoSwift.shiftLeft(self, by: count) //FIXME: count:
  43. }
  44. /** Shift bits to the right. All bits are shifted (including sign bit) */
  45. mutating func shiftRight(by count: Int) {
  46. if (self == 0) {
  47. return
  48. }
  49. let bitsCount = MemoryLayout<Int>.size * 8
  50. if (count >= bitsCount) {
  51. return
  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. }
  65. }
  66. // Left operator
  67. /** shift left and assign with bits truncation */
  68. func &<<= (lhs: inout Int, rhs: Int) {
  69. lhs.shiftLeft(by: rhs)
  70. }
  71. /** shift left with bits truncation */
  72. func &<< (lhs: Int, rhs: Int) -> Int {
  73. var l = lhs;
  74. l.shiftLeft(by: rhs)
  75. return l
  76. }
  77. // Right operator
  78. /** shift right and assign with bits truncation */
  79. func &>>= (lhs: inout Int, rhs: Int) {
  80. lhs.shiftRight(by: rhs)
  81. }
  82. /** shift right and assign with bits truncation */
  83. func &>> (lhs: Int, rhs: Int) -> Int {
  84. var l = lhs;
  85. l.shiftRight(by: rhs)
  86. return l
  87. }