Monday, February 8, 2016 [Tweets] [Favorites]

Dealing With Bit Sets in Swift

Umberto Raimondi (via Natasha Murashev):

In case of multiple integers in the same expression, Swift can only infer the type of free integer literals when they are used with some typed variable of one single type, as before, no implicit conversion toward the bigger integer type is performed.

[…]

The only drawback of this safety-first/no-assumptions approach, is that when you need to perform a lot of type conversions, you code starts to become bloated with all those truncating conversions.

But luckily, in Swift we can extend base types with new methods and we can use this to add some utility methods that truncate to a specific size to all the integer types[…]

[…]

All the alternative approaches to manipulate bit sets presented in this post are part of Bitter, a library that try to offer a more “swifty” interface for bit sets manipulation.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment