Skip to content

Instantly share code, notes, and snippets.

@chefnobody
Created March 2, 2018 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chefnobody/7b4048fc65e5297fe4a35d4b569b9ac8 to your computer and use it in GitHub Desktop.
Save chefnobody/7b4048fc65e5297fe4a35d4b569b9ac8 to your computer and use it in GitHub Desktop.
Playing around a little w/ binary representations of 8 bit numbers, bitwise operations and shifting.
import Foundation
/*
Bit manipulation in Swift
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html
*/
var zero: UInt8 = 0b00000000
var one: UInt8 = 0b00000001
var two: UInt8 = 0b00000010
var three: UInt8 = 0b00000011
var four: UInt8 = 0b00000100
var five: UInt8 = 0b00000101
var six: UInt8 = 0b00000110
var max: UInt8 = 0b11111111
/* & AND
bitwise &'s replace all 1/0 and 0/1 comparisons w/ 0
and leave all 1/1 bitwise matches in place.
By definition:
The bitwise AND operator (&) combines the bits of two numbers.
It returns a new number whose bits are set to 1 only if the bits were equal to 1 in both input
numbers.
Think of it like replacing all bits with zero, except when both bits in the inputs are 1.
*/
zero & zero
zero & one
zero & three
one & three
three & three
// note: order shouldn't matter
one & three
three & one
four & five
five & four
/* | OR
bitwise OR (|) compares each bit in two numbers and returns a number whose bits are 1 if the bits are set to one in either of the inputs.
Think of it like merging all the 1's together and leaving 0's only when both inputs have a 0 in the same bit position.
*/
zero | two
one | two
one | three
two | three
// note: order doesn't matter
three | two
three | one
five | five
five | zero
zero | five
/* ^ XOR
The bitwise XOR operator, or “exclusive OR operator” (^), compares the bits of two numbers.
The operator returns a new number whose bits are set to 1 where the input bits are different and are set to 0 where the input bits are the same:
Think of it like merging 0's where all bits (both 1's and 0's) match in the input, otherwise it merges a 1.
*/
zero ^ two
three ^ five
/* ~ NOT
The bitwise NOT operator (~) inverts all bits in a number:
Inverts all the bits in the number. Swaps 1's for 0's and 0's for 1's.
*/
~zero
~max
~three
~six
/*
Bitwise shift left (<< n) shifts the bits to the left by n positions.
- This has the effect of multiplying by 2 for each position moved. Or multiplying by 2n.
Bitwise shift right (>> n) shifts the bits to the right by n positions.
- This has the effect of dividing by 2 for each position moved. Or dividing by 2n.
Shifting Behavior for Unsigned Integers (logical shift):
The bit-shifting behavior for unsigned integers is as follows:
- Existing bits are moved to the left or right by the requested number of places.
- Any bits that are moved beyond the bounds of the integer’s storage are discarded.
- Zeros are inserted in the spaces left behind after the original bits are moved to the left or right.
*/
// left shift
zero << 1
one << 1
two << 1
three << 1
six << 1
one << 5 // 1 * 2 * 2 * 2 * 2 * 2 == 32
let result = four << 5 // 4 * 2 * 2 * 2 * 2 * 2 == 128
print(String(result, radix: 2))
// right shift
three >> 1 // rounds down to 1?
five >> 1 // rounds down to 2?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment