f-sharp
  1. f-sharp-bitwise-operators

F# Bitwise Operators

F# provides bitwise operators that can be used to perform bitwise operations on integral values. These operators can be used to manipulate individual bits of a value, perform logical operations, and perform shifting operations. In this article, we will see an overview of bitwise operators in F#.

Syntax

The following are the bitwise operators available in F#:

Operator Name Description
&&& Bitwise AND Returns a value where each bit is set if the corresponding bits of both values are set.
`
^^^ Bitwise XOR Returns a value where each bit is set if only one of the corresponding bits of both values are set.
~~~ Bitwise complement Inverts all the bits in a value.
<<< Left shift Shifts all the bits of a value to the left by a specified number of positions.
>>> Right shift Shifts all the bits of a value to the right by a specified number of positions, sign-extending the value if it is a signed integer.

Example

The following code demonstrates the use of bitwise operators in F#:

let x = 0b1100
let y = 0b1010

let andResult = x &&& y
let orResult = x ||| y
let xorResult = x ^^^ y
let complementResult = ~~~x
let leftShiftResult = x <<< 2
let rightShiftResult = x >>> 2

printfn "x:     %x" x
printfn "y:     %x" y
printfn "x &&& y: %x" andResult
printfn "x ||| y: %x" orResult
printfn "x ^^^ y: %x" xorResult
printfn "~~~x:    %x" complementResult
printfn "x <<< 2: %x" leftShiftResult
printfn "x >>> 2: %x" rightShiftResult

Output

The output of the above code will be:

x:     c
y:     a
x &&& y: 8
x ||| y: e
x ^^^ y: 6
~~~x:    fffffff3
x <<< 2: 30
x >>> 2: 3

Explanation

In the above example, we have created two variables x and y. These variables are of type int and are assigned binary values using the 0b prefix.

We have then used the bitwise operators to perform various operations on these values. The results are stored in separate variables.

Finally, we have printed the values of x, y, and the results of the bitwise operations using the %x format specifier to display the values in hexadecimal format.

Use

Bitwise operators are used in situations where bit-level manipulation of values is required. This can be useful in applications such as cryptography, networking, and device drivers.

Important Points

  • Bitwise operators are only applicable to integral types (byte, sbyte, int16, uint16, int32, uint32, int64, uint64).
  • The bitwise complement operator (~~~) inverts all the bits in a value.
  • The left shift operator (<<<) shifts all the bits of a value to the left by a specified number of positions. The right shift operator (>>>) shifts all the bits of a value to the right by a specified number of positions, sign-extending the value if it is a signed integer.
  • Bitwise operators have a lower precedence than arithmetic and comparison operators.

Summary

In this article, we have seen an overview of bitwise operators in F#. We have seen the syntax, example, output, explanation, use, important points, and summary of bitwise operators.

Published on: