Representing information as bits

At the smallest scale in the computer, information is stored as bits and bytes.

Bit

  • a “bit” is atomic: the smallest unit of storage
  • a bit stores just a 0 or 1
  • …

Why bits? Electronic Implementation

  • Easy to store with bistable elements
  • Reliably transmitted on noisy and inaccurate wires

image.png

Encoding Byte Values

Rather than accessing individual bits in memory, most computers use blocks of
8 bits, or bytes, as the smallest addressable unit of memory.

Byte

  • byte = 8 bits
  • Binary: Bits, Bytes and Integers Part 1 - 图4 to Bits, Bytes and Integers Part 1 - 图5
  • Decimal: Bits, Bytes and Integers Part 1 - 图6 to Bits, Bytes and Integers Part 1 - 图7
  • Hexadecimal: Bits, Bytes and Integers Part 1 - 图8 to Bits, Bytes and Integers Part 1 - 图9
    • Base 16 number representation
    • Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’
    • Write Bits, Bytes and Integers Part 1 - 图10 in C as
      • 0xFA1D37B
      • 0xfa1d37b

image.png

Bit-level manipulations

Since binary values are at the core of how computers encode, store, and manipu-
late information, a rich body of mathematical knowledge has evolved around the
study of the values 0 and 1. This started with the work of George Boole (1815–
1864) around 1850 and thus is known as Boolean algebra. Boole observed that by
encoding logic values true and false as binary values 1 and 0, he could formulate
an algebra that captures the basic principles of logical reasoning.

image.png

Bit-level Operations in C

image.png
As we can see, the best way to determine the effect of a bit-level expression is to expand the hexadecimal to their binary representations.

Note: there are some features contrast to logic operations (&&, ||, !)

  • View 0 as “False”
  • Anything nonzero as “True”
  • Always return 0 or 1
  • Early termination

Shift Operations in C

Left Shift: x << y

  • Shift bit-vector x left y positions
    • Throw away extra bits on left
    • Fill with 0 on right | Argument x | 01100010 | | —- | —- | | << 3 | 0010000 | | Log. >> 2 | 00011000 | | Arith. >> 2 | (00)011000 |

Right Shift: x >> y

  • Shift bit-vector x right y positions
    • Throw away extra bits on right
  • Logical shift
    • Fill with 0 on left
  • Arithmetic shift
    • Replicate most significant bit on left | Argument x | 10100010 | | —- | —- | | << 3 | 00010000 | | Log. >> 2 | 00101000 | | Arith. >> 2 | (11)101000 |

Undefined Behavior

  • Shift amount < 0 or Bits, Bytes and Integers Part 1 - 图14 word size

Integer Representations

In this section, we describe two different ways bits can be used to encode integers—
one that can only represent nonnegative numbers, and one that can represent
negative, zero, and positive numbers.

Unsigned Encodings

Definition of unsigned encoding for vector Bits, Bytes and Integers Part 1 - 图15:

Bits, Bytes and Integers Part 1 - 图16%3D%5Csum%7Bi%3D0%7D%5E%7Bw-1%7Dx_i%202%5Ei%0A#card=math&code=B2U_w%28%5Cvec%7Bx%7D%29%3D%5Csum%7Bi%3D0%7D%5E%7Bw-1%7Dx_i%202%5Ei%0A&height=47&width=128)

Note: Function Bits, Bytes and Integers Part 1 - 图17 is a bijection

Two’s-Complement Encodings

Definition of two’s-complement encoding for vector Bits, Bytes and Integers Part 1 - 图18:

Bits, Bytes and Integers Part 1 - 图19%3D-x%7Bw-1%7D2%5E%7Bw-1%7D%2B%5Csum%7Bi%3D0%7D%5E%7Bw-2%7Dxi%202%5Ei%0A#card=math&code=B2T_w%28%5Cvec%7Bx%7D%29%3D-x%7Bw-1%7D2%5E%7Bw-1%7D%2B%5Csum_%7Bi%3D0%7D%5E%7Bw-2%7Dx_i%202%5Ei%0A&height=47&width=218)

Note: Function Bits, Bytes and Integers Part 1 - 图20 is a bijection and most significant bit indicates sign (0 for nonnegative and 1 for negative)

Conversion and Casting

image.png

  • Bit pattern is maintained
  • But reinterpreted
  • Can have unexpected effects: adding or subtracting Bits, Bytes and Integers Part 1 - 图22
  • Expression containing signed and unsigned int

Expanding and Truncating

Sign Extension

Given Bits, Bytes and Integers Part 1 - 图23 bits signed integer x, convert it to Bits, Bytes and Integers Part 1 - 图24 bits integer with same value

Bits, Bytes and Integers Part 1 - 图25 Make Bits, Bytes and Integers Part 1 - 图26 copies of sign bit:

Bits, Bytes and Integers Part 1 - 图27

image.png

  • Unsigned: zeros added
  • Signed: sign extension
  • Both yield expected result

Truncation

Given Bits, Bytes and Integers Part 1 - 图29 bits signed or unsigned integer x, convert it to Bits, Bytes and Integers Part 1 - 图30 bits integer Bits, Bytes and Integers Part 1 - 图31 with same value for “small enough” x

Bits, Bytes and Integers Part 1 - 图32 Drop top Bits, Bytes and Integers Part 1 - 图33 bits:

Bits, Bytes and Integers Part 1 - 图34
image.png

  • Unsigned/signed: bits are truncated
  • Result reinterpreted
  • Unsigned: mod operation
  • Signed: similar to mod
  • For small numbers yields expected behavior