Unsigned Integers in Binary

Binary Intro

Binary is a base two numbering system where each “bit” can have a value of either zero (0) or one (1). A byte is made up of eight (8) bits. Each byte has a total of 256 or 28 possible combinations. Thinking in numerical representation of a byte, you can assign each possible byte a number between 0 and 256. The least significant bit has a value of 20 and the most significant has a value of 27.

An unsigned integer (unsigned means it can only be positive) with the value of 519 could be represented in binary as 00000010 00000111. This would put the theoretical maximum two byte integer value at 216 or 65,535. For the purposes of this document, I will be focusing on 8-bit, unsigned integers.

[[Binary Calculation of Signed Integers]] follow a rule known as Two’s Compliment to allow positive and negative integers to be added using the same bitwise addition.

Decimal to Binary Conversion:

Most Significant Bit = Leftmost Bit Least Significant Bit = Rightmost Bit

Least Significant Bit = 20 = 1 Second Significant Bit = 21 = 2 Third Significant Bit = 22 = 4 Fourth Significant Bit = 23 = 8 Fifth Significant Bit = 24 = 16 Sixth Significant Bit = 25 = 32 Seventh Significant Bit = 26 = 64 Eighth Significant Bit = 27 = 128

1286432168421Total
11001011203
0010100040
0001010121
Formula: 2n-1 where n is the number of positions from the right.

Example: 155 → Binary

  1. Start with the most significant bit as a 1 and all others zero. 1 0 0 0 0 0 0 0 = 128

  2. Add next most significant bit and compare total to 155 1 1 0 0 0 0 0 0 = 192

  3. If the total is greater than 155, change the bit to a zero and move on to the next. 1 0 1 0 0 0 0 0 = 140

  4. Repeat steps 2 & 3 until you reach 155. 1 0 1 1 0 0 0 0 = 156 1 0 1 0 1 0 0 0 = 148 1 0 1 0 1 1 0 0 = 152 1 0 1 0 1 1 1 0 = 154 1 0 1 0 1 1 1 1 = 155

Bitwise Operations

Bitwise operators work at the bit level. This means that rather than working on the logical representation of the data they are acting on, they work on the binary representation of that data.

Comparison Operators

Comparison operators take two bytes as arguments.

LogicOperatorDescription
AND&Returns 1 if both bits are 1, otherwise returns 0
OR|Returns 1 if either bit is 1, otherwise returns 0
XOR^Returns 1 if one bit or the other is 1, but not both

Modification Operators

Modification operators act on a single byte.

LogicOperatorDescription
NOT~Flips bit values of each bit in the byte
SHIFT LEFT<<Shifts bit pattern left and pads with zeroes
SHIFT RIGHT>>Shifts bit pattern right and drops the rightmost bit

References