_ _ _ _ ^ one's place ^ two's place ^ four's place ^ eight's placeAs discussed before integers come in different sizes.
type number of bits (places) char 8 short 16 int 32 long 64Conversion from Binary to Decimal.
Example: Convert 01101011 Binary to Decimal.
Work from left to right. Start with sum=0. For each digit, double the sum and add the digit into the sum.
Working let to right. pattern: sum *= 2; sum += d; where d is the digit sum *= 2; sum += 0; so sum=0 sum *= 2; sum += 1; so sum=1 sum *= 2; sum += 1; so sum=3 sum *= 2; sum += 0; so sum=6 sum *= 2; sum += 1; so sum=13 sum *= 2; sum += 0; so sum=26 sum *= 2; sum += 1; so sum=53 sum *= 2; sum += 1; so sum=107 107 Decimal is the equivalent value.Convert from Decimal to Binary
Example: Convert 297 Decimal to Binary. Work from right to left.
Start with num=297. Divide by 2. The remainder is the next binary digit.
The quotient is tne new value for num. Stop when the quotient is 0.
Below the expression num%2
is the remainder upon dividing num by
2. Variable num
is unchanged. The expression
num/=2
does change num
. The new
num
is the (whole) number of times that 2 divides into the old
num
.
num=297 Binary = --------- r = num%2 = 1 q = (num /= 2) = 148 Binary = --------1 r = num%2 = 0 q = (num /= 2) = 74 Binary = -------01 r = num%2 = 0 q = (num /= 2) = 37 Binary = ------001 r = num%2 = 1 q = (num /= 2) = 18 Binary = -----1001 r = num%2 = 0 q = (num /= 2) = 9 Binary = ----01001 r = num%2 = 1 q = (num /= 2) = 4 Binary = ---101001 r = num%2 = 0 q = (num /= 2) = 2 Binary = --0101001 r = num%2 = 0 q = (num /= 2) = 1 Binary = -00101001 r = num%2 = 1 q = (num /= 2) = 0 Binary = 100101001
a & b a | b a ^ b ~aThe & (and) operation, the | (or) operation, and the ^ (exclusive or) operation apply to corresonding bits in a and b. The result of & is 0 unless both corresponding bits are 1. The result of | is 1 unless both corresponding bits are 0. The result of ^ is 0 unless corresponding bits are different. The result of ~ is to make 0 bits into 1 bits and 1 bits into 0 bits.
Example.
a = 18 = 00010010 Binary b = 6 = 00000110 Binary a & b = 00000010 Binary = 2 a | b = 00010110 Binary = 22 a ^ b = 00010100 Binary = 20 ~a = 11101101 Binary = 237
Example.
a = b = 18 = 00010010 Binary a |= 8; //sets bit 3 of a; note 8 is 2 raised to exponent 3; a is now 26 b &= (~16); //clears bit 4 of b; Note 16 is 2 raised to exponent 4; b is now 2
expression >> shift_amount expression << shift_amountOperation >> is a shift right of the bits of the expression.
a = 18 = 00010010 Binary a >> 2The value is the value of the expression but shifted two bits to the right. The two right-most bits are lost; Two new 0 bits come in on the left
a >> 2 = 00000100 Binary = 4Operation << is a shift left of the bits of the expression.
a = 18 = 00010010 Binary a << 4The value is the value of the expresssion but shifted 4 bits left. 4 0's are brought in on the right and the left-most 4 bits are lost.
a << 4 = 00100000 Binary = 32next: More Control (Constructs)