On this page:
byte
byte-ref
byte-clear-leftmost-bits
byte-clear-rightmost-bits
byte-drop-rightmost-bits
byte-and
byte-or
byte-not
byte-xor
byte-nand
byte-nor
byte-xnor
in-byte
into-byte
byte-hamming-weight
8.12

6.3 Bytes🔗ℹ

 (require rebellion/binary/byte) package: rebellion

A byte is a sequence of eight bits, represented as an exact integer between 0 and 255.

procedure

(byte a b c d e f g h)  byte?

  a : bit?
  b : bit?
  c : bit?
  d : bit?
  e : bit?
  f : bit?
  g : bit?
  h : bit?
Constructs a byte from the eight given bits.

Examples:
> (byte 0 0 0 0 0 0 0 0)

0

> (byte 1 1 1 1 1 1 1 1)

255

> (byte 0 0 1 0 0 0 0 0)

32

> (byte 1 0 0 0 0 0 0 1)

129

procedure

(byte-ref b pos)  bit?

  b : byte?
  pos : (integer-in 0 7)
Returns the bit at index pos in b. Indices are numbered from left to right and start at zero.

Examples:
> (byte-ref (byte 0 0 1 1 0 1 0 1) 0)

0

> (byte-ref (byte 0 0 1 1 0 1 0 1) 2)

1

> (byte-ref (byte 0 0 1 1 0 1 0 1) 7)

1

procedure

(byte-clear-leftmost-bits b num-bits)  byte?

  b : byte?
  num-bits : (integer-in 0 8)
Sets the left num-bits bits of b to zero.

Examples:
> (byte-clear-leftmost-bits (byte 1 1 1 0 1 0 1 0) 2)

42

> (byte-clear-leftmost-bits (byte 1 1 1 0 1 0 1 0) 5)

2

> (byte-clear-leftmost-bits (byte 1 1 1 0 1 0 1 0) 8)

0

procedure

(byte-clear-rightmost-bits b num-bits)  byte?

  b : byte?
  num-bits : (integer-in 0 8)
Sets the right num-bits bits of b to zero.

Examples:
> (byte-clear-rightmost-bits (byte 1 1 1 0 1 0 1 0) 2)

232

> (byte-clear-rightmost-bits (byte 1 1 1 0 1 0 1 0) 4)

224

> (byte-clear-rightmost-bits (byte 1 1 1 0 1 0 1 0) 7)

128

procedure

(byte-drop-rightmost-bits b num-bits)  byte?

  b : byte?
  num-bits : (integer-in 0 8)
Deletes the rightmost num-bits of b, shifting all remaining bits to the right and inserting zeros to the left.

Examples:
> (byte-drop-rightmost-bits (byte 1 1 1 0 1 0 1 0) 2)

58

> (byte-drop-rightmost-bits (byte 1 1 1 0 1 0 1 0) 4)

14

> (byte-drop-rightmost-bits (byte 1 1 1 0 1 0 1 0) 7)

1

procedure

(byte-and left right)  byte?

  left : byte?
  right : byte?
Performs a bitwise and operation on left using right as the mask. Can be used to zero out specific bits of a byte.

Examples:
; extract the least-significant bit.
> (byte-and (byte 1 1 1 1 1 1 1 1) (byte 0 0 0 0 0 0 0 1))

1

; extract bits 7, 5, and 0.
> (byte-and (byte 1 1 1 1 1 1 1 1) (byte 1 0 1 0 0 0 0 1))

161

procedure

(byte-or left right)  byte?

  left : byte?
  right : byte?
Performs a bitwise or operation on left using right as the mask. Can be used to set specific bits of a byte without affecting others.

Examples:
; set the least-significant bit
> (byte-or (byte 1 0 0 0 0 0 0 0) (byte 0 0 0 0 0 0 0 1))

129

; set bits 5, 2, and 1
> (byte-or (byte 0 0 1 0 1 0 1 0) (byte 0 0 1 0 0 0 1 1))

43

procedure

(byte-not b)  byte?

  b : byte?
Performs a bitwise not operation on b, inverting all bits.

Examples:
> (byte-not (byte 1 1 1 1 1 1 1 1))

0

> (byte-not (byte 0 0 0 0 0 0 0 0))

255

procedure

(byte-xor left right)  byte?

  left : byte?
  right : byte?
Performs a bitwise xor (exclusive or, also known as eor) operation on left using right as the mask.

Examples:
> (byte-xor (byte 0 0 0 1 0 1 0 1) (byte 0 0 1 1 1 1 1 1))

42

; swap x and y without a temporary variable
> (let ([x 42] [y 23])
    (set! x (byte-xor x y))
    (set! y (byte-xor y x))
    (set! x (byte-xor x y))
    (list x y))

'(23 42)

procedure

(byte-nand left right)  byte?

  left : byte?
  right : byte?
Performs a bitwise nand operation on left using right as the mask. Equivalent to (byte-not (byte-and left right)).

Examples:
> (byte-nand (byte 1 1 1 1 1 1 1 1) (byte 1 1 1 1 1 1 1 1))

0

> (byte-not (byte-and (byte 1 1 1 1 1 1 1 1) (byte 1 1 1 1 1 1 1 1)))

0

> (byte-nand (byte 0 0 0 0 0 0 0 0) (byte 0 0 0 0 0 0 0 0))

255

> (byte-not (byte-and (byte 0 0 0 0 0 0 0 0) (byte 0 0 0 0 0 0 0 0)))

255

procedure

(byte-nor left right)  byte?

  left : byte?
  right : byte?
Performs a bitwise nor operation on left using right as the mask. Equivalent to (byte-not (byte-or left right)).

Examples:
> (byte-nor (byte 1 1 1 1 1 1 1 1) (byte 1 1 1 1 1 1 1 1))

0

> (byte-not (byte-or (byte 1 1 1 1 1 1 1 1) (byte 1 1 1 1 1 1 1 1)))

0

> (byte-nor (byte 0 0 0 0 0 0 0 0) (byte 0 0 0 0 0 0 0 0))

255

> (byte-not (byte-or (byte 0 0 0 0 0 0 0 0) (byte 0 0 0 0 0 0 0 0)))

255

procedure

(byte-xnor left right)  byte?

  left : byte?
  right : byte?
Performs a bitwise xnor operation on left using right as the mask. Equivalent to (byte-not (byte-xor left right)).

Examples:
> (byte-xnor (byte 1 1 1 1 1 1 1 1) (byte 1 1 1 1 1 1 1 1))

255

> (byte-not (byte-xor (byte 1 1 1 1 1 1 1 1) (byte 1 1 1 1 1 1 1 1)))

255

> (byte-xnor (byte 0 0 0 0 0 0 0 0) (byte 0 0 0 0 0 0 0 0))

255

> (byte-not (byte-xor (byte 0 0 0 0 0 0 0 0) (byte 0 0 0 0 0 0 0 0)))

255

procedure

(in-byte b)  sequence?

  b : byte?
Returns a sequence whose elements are equivalent to the list of bits that comprise b.

Examples:
; convert byte to a vector of bits
> (for/vector ([b (in-byte 193)])
    b)

'#(1 1 0 0 0 0 0 1)

A reducer that combines a sequence of eight bits into a single byte. Any remaining bits are ignored.

Examples:
> (reduce into-byte 0 0 0 0 0 0 0 0)

0

> (reduce into-byte 1 1 1 1 1 1 1 1)

255

> (reduce into-byte 0 1 1 1 0 1 0 1)

117

> (reduce into-byte 0 1 1 1 0 1 0 1 1 1 1 1 1 1)

117

procedure

(byte-hamming-weight b)  (integer-in 0 8)

  b : byte?
Returns the Hamming weight of the byte. This is the same as how many 1’s are in the byte. See the Wikipedia page for more informating on Hamming weights.

Examples:
; count the number of 1's in the byte
> (byte-hamming-weight (byte 1 1 1 1 0 0 0 1))

5

> (byte-hamming-weight (byte 0 0 0 0 0 0 0 0))

0

> (byte-hamming-weight (byte 1 1 1 1 1 1 1 1))

8