even
odd
even
odd
64 + 32 + 16 + 8 + 4 + 2 + 1 = 127
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
32 + 8 + 2 = 42
16 + 8 + 2 + 1 = 27
0001 1111 1110 1111 0100 0101 0101 1011 0110 1 F E F 4 5 5 B 6 0001 0010 1001 0101 0100 1110 1111 1101 1101 1 2 9 5 4 E F D D 0001 0010 1010 0101 0101 0010 1010 1010 1010 1 2 A 5 5 2 A A A 0001 0111 1111 1111 1111 1111 1111 1111 1111 1 7 F F F F F F F 0111 1111 7 F 1111 1111 F F 0010 1010 2 A 0001 1011 1 B
0 to (2N - 1), or 2N numbers
0001 1011 ----- + 1100
1011011 0110101 -------- + 0010000
-0
+0
-(16 + 4 + 2) = -22
The leftmost bit is for the sign, so only the remaining 6 bits can be for the magnitude of the numbers. For the positives, there are: 000000 to 111111, (+0 to +63) 0 to 26 - 1, which is 26 = 64 numbers For the negatives, there are also 64 numbers (-63 to -0). Overall there are 128 numbers that are represented.
bias = 27 - 1 = 26 = 64
64 + 32 + 16 + 4 + 2 = 118 - bias = 118 - 64 = 54
8 + 2 + 1 = 11 - bias = 11 - 64 = -53
0000000 to 1111111, or -64 to +63 which is -64 to -1 (64 numbers) 0 to +63 (64 numbers) or 64 + 64 = 128 numbers
The biased representation can't store +64. And only has one representation for zero.
smallest = 1000000 = -64 biggest = 0111111 = +63
There are 128 possible numbers: -64 to -1 0 to +63
0101010 1010110 -------- + 0000000
0111111 1101011 -------- + 0101010
4 + 2 + 1 + 0.5 + 0.25 + 0.125 = 7.875
-(0.25 + 0.0625) = -0.3125
111.111 = 0.111111 * 23 -0.0101 = -0.101 * 2-1
111.111 = [0 | 1011 | 11111] -0.0101 = [1 | 0111 | 01000]
[0/1 | 0000 | 00000] = (+/-) 0.1 * 2-8 = (+/-) 0.000000001 = (+/-) 2-9 = (+/-) 0.001953125 [0/1 | 1111 | 11111] = (+/-) 0.111111 * 2+7 = 1111110.0 = (+/-) 126
#include <stdlib.h> #include <iostream.h> void main(int argc, char* argv[]) { int get_positive_integer(int, char**); void calculate_binary_number(int); void display_usage_message(void); int theInteger; theInteger = get_positive_integer(argc,argv); if (theInteger >= 0) { calculate_binary_number(theInteger); cout << endl; } else { display_usage_message(); cout << endl; } } void calculate_binary_number(int theInteger) { int remainder = theInteger % 2; int quotient = theInteger / 2; cout << "For theInteger = " << theInteger << " remainder is " << remainder << " quotient is " << quotient << endl; if ((remainder == 0) && (quotient == 0)) return; else { calculate_binary_number(quotient); if (remainder == 1) cout << '1'; else cout << '0'; } } int get_positive_integer(int argc, char* argv[]) { if (argc != 2) return -1; else return atoi(argv[1]); } void display_usage_message(void) { cout << "Usage: foo number" << endl; cout << "number must be a positive integer" << endl; }
The program takes a single command-line argument, checks that the argument is a positive integer, and then proceeds to calculate the binary representation for the positive integer. If there are too many arguments, or no arguments, or the argument is not a positive integer, a usage message is displayed. (Note that atoi returns 0 if the argument is not a number, also if the argument is a fixed point number, only the whole number part will be used.) The binary number is generated in reverse. As long as the quotient and remainder of theInteger are both not 0 then recursion happens. The recursive calls are also before the cout statements for 1 and 0, thus the output will be reversed. But the binary number's digits are calculated in reverse; the net effect is an output of 0's and 1's for the binary number in normal order.
1023 / 2 = 511 and 1, need 1 in the 20 bit 511 / 2 = 255 and 1, need 1 in the 21 bit 255 / 2 = 127 and 1, need 1 in the 22 bit 127 / 2 = 63 and 1, need 1 in the 23 bit 63 / 2 = 31 and 1, need 1 in the 24 bit 31 / 2 = 15 and 1, need 1 in the 25 bit 15 / 2 = 7 and 1, need 1 in the 26 bit 7 / 2 = 3 and 1, need 1 in the 27 bit 3 / 2 = 1 and 1, need 1 in the 28 bit 1 / 2 = 0 and 1, need 1 in the 29 bit 0 / 2 = 0 and 0, stop reverse this list, and get: 1111111111
27 / 2 = 13 and 1, need 1 in the 20 bit 13 / 2 = 6 and 1, need 1 in the 21 bit 6 / 2 = 3 and 0, put 0 in the 22 bit 3 / 2 = 1 and 1, need 1 in the 23 bit 1 / 2 = 0 and 1, need 1 in the 24 bit 0 / 2 = 0 and 0, stop reverse this list, and get: 11011
Consider 13: 13 / 2 = 6 and 1, place 1 at bit 0 6 / 2 = 3 and 0, place 0 at bit 1 3 / 2 = 1 and 1, place 1 at bit 2 1 / 2 = 0 and 1, place 1 at bit 3 0 / 2 = 0 and 0, stop reverse this list, and get: 1101 Consider 0.703125: 0.703125 * 2 = 1 and .40625, place 1 at bit -1 0.40625 * 2 = 0 and .8125, place 0 at bit -2 0.8125 * 2 = 1 and .625, place 1 at bit -3 0.625 * 2 = 1 and .25, place 1 at bit -4 0.25 * 2 = 0 and .5, place 0 at bit -5 0.5 * 2 = 1 and 0, place 1 at bit -6 0 * 2 = 0 and 0, stop use this list, and get: 0.101101 overall 13.703125 = 1101.101101