Bits and Bytes

Before we get into more advanced commands, let’s talk about bits and bytes. All data stored or processed by a computer is binary. 8 binary digits, or bits, make up a byte. A single byte can store 2^8 or 256 different values, or an unsigned integer between 0-255.

0 =

If we want to work with bigger numbers, we need more bytes. With two bytes we can store a number as big as 65535. With two byte values, we refer to byte with the most significant bits as the high-order byte, and the byte with the least significant bits as the low-order byte.

High-order byte
Low-order byte

Generally the high-order byte comes first, but not always. This is called big-endian. If the low-order byte comes first it is called little-endian. There is also mixed-endian. We’re not going to dive any deeper into endianness, but we will look at chopping up bytes and shifting bits around using bitwise operators.

The & (and) operator compares the individual bits of two values. If both bits are 1, then the corresponding bit in the result will be 1. Otherwise it will be zero. This is useful for isolating a range of bits, and wiping out all others.

a =
1
1
0
1
1
0
0
1
0
1
0
1
1
1
0
1
b =
a & b =

Similarly, the | (or) operator compares the individual bits and if either is 1, the corresponding bit in the result is 1, otherwise zero. This can be useful for combining different ranges of bits into a byte or bytes.

a =
1
1
0
1
1
0
0
1
b =
0
1
0
1
1
1
0
1
a | b =
1
1
0
1
1
0
0
1
0
1
0
1
1
1
0
1

The << (shift left) and >> (shift right) operators shift the bits in that direction. Bits drop off at the ends and gaps are replaced with zeros.

Keywords

  • Await 1
  • B-Splines 1
  • Bezier 1
  • Binary 1
  • Bluetooth 1
  • Canvas 1 2
  • Curves 1
  • Drupal 1
  • Gulp 1
  • JavaScript 1 2 3
  • PHP 1
  • Promises 1
  • Xdebug 1