F# Bitwise Operators

F# Bitwise Operators

Bitwise operators are essential in low-level programming and are fundamental tools for manipulating individual bits within binary data. In the context of F#, a functional-first programming language developed by Microsoft, bitwise operators are indispensable for performing efficient bitwise operations on integers. In this article, we’ll explore the bitwise operators available in F# and understand their applications through comprehensive code examples.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Introduction to Bitwise Operators

Bitwise operators are used to perform operations at the bit level, directly manipulating the binary representation of integers. In F#, the following bitwise operators are available:

  • &&& (Bitwise AND)
  • ||| (Bitwise OR)
  • ^^^ (Bitwise XOR)
  • <<< (Left shift)
  • >>> (Right Shift)
  • ~~~ (Ones’ complement)

Let’s explore each of these operators to understand their functionality and use cases.

Bitwise AND (&&&)

The bitwise AND operator (&&&) performs a bitwise AND operation between corresponding bits of two integers. It results in a new integer where each bit is set to 1 only if the corresponding bits in both operands are 1.

[<EntryPoint>]
let main(args: string[]): int =

    let a: int =  13 // 0b1101
    let b: int =  10 // 0b1010
    
    let result: int = a &&& b // 0b1000 (8 in decimal)
    
    printfn "Result of %d &&& %d is %d." a b result
    
    0 // Return exit code 0 to indicate successful execution

In this example, the program will output: “Result of 13 &&& 10 is 8.”

Bitwise OR (|||)

The bitwise OR operator (|||) performs a bitwise OR operation between corresponding bits of two integers. It results in a new integer where each bit is set to 1 if at least one of the corresponding bits in the operands is 1.

[<EntryPoint>]
let main(args: string[]): int =

    let a: int =  13 // 0b1101
    let b: int =  10 // 0b1010
    
    let result: int = a ||| b // 0b1111 (15 in decimal)
    
    printfn "Result of %d ||| %d is %d." a b result
    
    0 // Return exit code 0 to indicate successful execution

In this example, the program will output: “Result of 13 ||| 10 is 15.”

Bitwise XOR (^^^)

The bitwise XOR operator (^^^) performs a bitwise exclusive OR operation between corresponding bits of two integers. It results in a new integer where each bit is set to 1 only if the corresponding bits in the operands are different.

[<EntryPoint>]
let main(args: string[]): int =

    let a: int =  13 // 0b1101
    let b: int =  10 // 0b1010
    
    let result: int = a ^^^ b // 0b0111 (7 in decimal)
    
    printfn "Result of %d ^^^ %d is %d." a b result
    
    0 // Return exit code 0 to indicate successful execution

In this example, program will output: “Result of 13 ^^^ 10 is 7.”

Left Shift (<<<)

The left shift operator (<<<) shifts the bits of an integer to the left by a specified number of positions. This is equivalent to multiplying the integer by 2 raised to the power of the shift amount.

[<EntryPoint>]
let main(args: string[]): int =

    let a: int =  13 // 0b1101
    let shiftAmount: int =  2 // Equivalent to (13 * 2) * 2
    
    let result: int = a <<< shiftAmount // 0b00110100 (52 in decimal)
    
    printfn "Result of %d <<< %d is %d." a shiftAmount result
    
    0 // Return exit code 0 to indicate successful execution

In this example, the program will output: “Result of 13 <<< 2 is 52.”

Right Shift (>>>)

The right shift operator (>>>) shifts the bits of an integer to the right by a specified number of positions. This is equivalent to dividing the integer by 2 raised to the power of the shift amount.

[<EntryPoint>]
let main(args: string[]): int =

    let a: int =  13 // 0b1101
    let shiftAmount: int =  2 // Equivalent to (13 / 2) / 2
    
    let result: int = a >>> shiftAmount // 0b0011 (3 in decimal)
    
    printfn "Result of %d >>> %d is %d." a shiftAmount result
    
    0 // Return exit code 0 to indicate successful execution

In this example, the program will output: “Result of 13 >>> 2 is 3.”

Bitwise NOT (~~~)

The bitwise NOT operator (~~~) inverts each bit of an integer, changing 1s to 0s and 0s to 1s.

[<EntryPoint>]
let main(args: string[]): int =

    let a: int =  13 // 0b1101
    
    let result = ~~~a // 0b11110010 (-14 in decimal)
    
    printfn "Result of ~~~%d is %d." a result
    
    0 // Return exit code 0 to indicate successful execution

In this example, the program will output: “Result of ~~~13 is -14.” The results may seem absurd, but this is due to two’s complement representation.

Conclusion

Mastering F# bitwise operators is essential for any programmer dealing with low-level operations, efficient data manipulation, or cryptographic algorithms. This guide has provided a comprehensive overview of the bitwise operators available in F#, along with practical examples to solidify your understanding.

Scroll to Top