2 ms·
Swift (https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html# https://docs.swift.org/swift-book/LanguageGuide/AdvancedOper...) uses a = a &+
by Someone 4y ago
Swift (https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html# https://docs.swift.org/swift-book/LanguageGuide/AdvancedOper...) uses
a = a &+ 1
or the shorthand
a &+= 1
IMO, that looks ugly, but that probably is a matter of getting used to it.
Compared to the %256 option, it has the advantage that, if you change the type of a, you won’t have to remember to change the modulus.
They also chose to not make modular integers separate types. That makes mixing ‘normal’ and modular arithmetic on integers easier (with modular types, you’d have to convert between regular and modular integers all the time) (edit: that also is consistent with the fact that the bitwise operators work with regular ints, and ook not require a special “set of 8/16/32/…” type that’s implemented as an integer)
I wouldn’t know how common such mixing is and, hence, whether that is a good choice.