Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Operators precedence

by fragi
March 2, 2022
in Algorithms
Share on FacebookShare on Twitter

Operators precedence matters when we have many operators in one expression. The operator with the highest precedence will be evaluated first.
The order of the operators precedence it is different from language to language. For Swift you can find many examples here: https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html 

When we are reading some sudo code or code from another language we have to pay attention on the operators order. Let’s have a look on an example of the Bech32 algorithm https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki

Here we can find the following method:

Swift
1
2
3
4
def bech32_create_checksum(hrp, data):
  values = bech32_hrp_expand(hrp) + data
  polymod = bech32_polymod(values + [0,0,0,0,0,0]) ^ 1
  return [(polymod >> 5 * (5 - i)) & 31 for i in range(6)]

If we try to implement the same method in Swift we have to be careful with the operators precedence otherwise the algorithm will not be correct.

Instead of writing:

Swift
1
2
3
4
5
6
7
8
9
    func checksum(hrp: String, data: [UInt8]) -> [UInt8] {
        let values = bech32_hrp_expand(hrp) + data
        let polymod = bech32_polymod(values + [0,0,0,0,0,0]) ^ 1
        var result: [UInt] = []
        for i in (0..<6) {
            result.append((polymod >> 5 * (5 - UInt(i))) & 31)
        }
        return result.map { UInt8($0) }
    }

we have to write:

Swift
1
2
3
4
5
6
7
8
9
    func checksum(hrp: String, data: [UInt8]) -> [UInt8] {
        let values = bech32_hrp_expand(hrp) + data
        let polymod = bech32_polymod(values + [0,0,0,0,0,0]) ^ 1
        var result: [UInt] = []
        for i in (0..<6) {
            result.append((polymod >> (5 * (5 - UInt(i)))) & 31)
        }
        return result.map { UInt8($0) }
    }

 

Otherwise the algorithm is not correct!

 

fragi

fragi

Related Posts

Swift – Disjoint Set (Union-Find)
Algorithms

Swift – Disjoint Set (Union-Find)

February 21, 2024

Disjoint Set is an algorithm that can help us solve many graph problems. There are many variations/improvements on the base...

Blake2b Algorithm
Algorithms

Bech32 Algorithm

March 1, 2022

Bech32 is a checksummed base32 format. It is being used in cryptocurrencies development (bitcoin, cardano,...) https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki, https://cips.cardano.org/cips/cip19/. Let's explore the...

Blake2b Algorithm
Algorithms

Blake2b Algorithm

February 28, 2022

Blake2 is a cryptographic hash function. https://www.blake2.net/ Let's now explore how to implement this algorithm in Swift. The first option...

Memoization
Algorithms

Memoization

February 15, 2022

Memoization is a technique to increase the time performance of an algorithm by using some extra memory. According to Wikipedia:...

Performance testing in Xcode
Algorithms

Performance testing in Xcode

February 15, 2022

The performance is most of the times very important. In this post we will explore we can do performance testing...

BIP39
Algorithms

BIP39

February 12, 2022

In this article we will talk about the BIP39 Algorithm that is being used in many crypto. wallets. So what...

Next Post
Delete Derived Data – zsh command

Delete Derived Data - zsh command

Swift – Disjoint Set (Union-Find)

Swift - Disjoint Set (Union-Find)

Heap

Heap

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.