HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulelens-5.3.5Haskell2010

Data.Bits.Lens

  • 16 values
  • Packagelens-5.3.5
  • Exports16
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceLens.hs
value(<<.|.~) :: Bits a => Optical' (->) q (Tuple2 a) s a -> a -> q s (a, s)
#

Bitwise .|. the target(s) of a Lens or Traversal, and return the original value, or a monoidal summary of the original values.

When you do not need the old value, (.|.~) is more flexible.

Example1 expression
_2 <<.|.~ 6 $ ("hello", 3)(3,("hello",7))
(<<.|.~) ::  Bits a            => Iso s t a a       -> a -> s -> (a, t)
(<<.|.~) ::  Bits a            => Lens s t a a      -> a -> s -> (a, t)
(<<.|.~) :: (Bits a, Monoid a) => Traversal s t a a -> a -> s -> (a, t)
value(<<.&.~) :: Bits a => Optical' (->) q (Tuple2 a) s a -> a -> q s (a, s)
#

Bitwise .&. the target(s) of a Lens or Traversal, and return the original value, or a monoidal summary of the original values.

When you do not need the old value, (.&.~) is more flexible.

Example1 expression
_2 <<.&.~ 7 $ ("hello", 254)(254,("hello",6))
(<<.&.~) ::  Bits a            => Iso s t a a       -> a -> s -> (a, t)
(<<.&.~) ::  Bits a            => Lens s t a a      -> a -> s -> (a, t)
(<<.&.~) :: (Bits a, Monoid a) => Traversal s t a a -> a -> s -> (a, t)
value(<<.|.=) :: (MonadState s m, Bits a) => LensLike' (Tuple2 a) s a -> a -> m a
#

Modify the target(s) of a Lens', (or Traversal') by computing its bitwise .|. with another value, returning the original value (or a monoidal summary of all the original values).

When you do not need the old value, (.|.=) is more flexible.

Example1 expression
runState (_1 <<.|.= 7) (28,0)(28,(31,0))
(<<.|.=) :: (MonadState s m, Bits a)           => Lens' s a      -> a -> m a
(<<.|.=) :: (MonadState s m, Bits a, Monoid a) => Traversal' s a -> a -> m a
value(<<.&.=) :: (MonadState s m, Bits a) => LensLike' (Tuple2 a) s a -> a -> m a
#

Modify the target(s) of a Lens', (or Traversal') by computing its bitwise .&. with another value, returning the original value (or a monoidal summary of all the original values).

When you do not need the old value, (.&.=) is more flexible.

Example1 expression
runState (_1 <<.&.= 15) (31,0)(31,(15,0))
(<<.&.=) :: (MonadState s m, Bits a)           => Lens' s a      -> a -> m a
(<<.&.=) :: (MonadState s m, Bits a, Monoid a) => Traversal' s a -> a -> m a
valuebitAt :: Bits b => Int -> IndexedLens' Int b Bool
#

This Lens can be used to access the value of the nth bit in a number.

bitAt n is only a legal Lens into b if 0 <= n < bitSize (undefined :: b).

Example1 expression
16^.bitAt 4True
Example1 expression
15^.bitAt 4False
Example1 expression
15 & bitAt 4 .~ True31
Example1 expression
16 & bitAt 4 .~ False0
valuebits :: (Num b, Bits b) => IndexedTraversal' Int b Bool
#

Traverse over all bits in a numeric type.

The bit position is available as the index.

Example1 expression
toListOf bits (5 :: Word8)[True,False,True,False,False,False,False,False]

If you supply this an Integer, the result will be an infinite Traversal, which can be productively consumed, but not reassembled.

valuebyteAt :: (Integral b, Bits b) => Int -> IndexedLens' Int b Word8
#

Get the nth byte, counting from the low end.

byteAt n is a legal Lens into b iff 0 <= n < div (bitSize (undefined :: b)) 8

Example1 expression
(0xff00 :: Word16)^.byteAt 00
Example1 expression
(0xff00 :: Word16)^.byteAt 1255
Example1 expression
byteAt 1 .~ 0 $ 0xff00 :: Word160
Example1 expression
byteAt 0 .~ 0xff $ 0 :: Word16255

Traverse over all the bytes in an integral type, from the low end.

The byte position is available as the index.

Example1 expression
toListOf bytewise (1312301580 :: Word32)[12,34,56,78]

If you supply this an Integer, the result will be an infinite Traversal, which can be productively consumed, but not reassembled.

Why isn't this function called bytes to match bits? Alas, there is already a function by that name in Data.ByteString.Lens.