emacs binhex.c
Remember to use Ctrl-x Ctrl-s to save your file, Ctrl-x Ctrl-c to exit, and Ctrl-g to cancel a partially typed or accidental command. Save your code frequently.
void powers_of_2()
{
int i;
unsigned int ipower;
for (i = 0; i < 32; i++)
{
// assign to ipower the value of 2 raised to power i
// print ipower
}
}
gcc binhex.c -o xbinhex
gcc (which stands for GNU Compiler Collection) is the name of the compiler. The first argument binhex.c is the name of the C program you wish to compile. The option –o informs the compiler that the argument to follow (in our case xbinhex) is the name of the executable program to be produced as result of the compilation. You can give any name you wish to the executable, but be sure to remember it. Pay careful attention to error and warning messages, and eliminate them all.
./xbinhex
decimal_to_binary(25);
should produce the output
00000000000000000000000000011001
void decimal_to_binary(int value)
{
// print the binary representation of value
}
unsigned int mask = 0x00000001;
if (value & mask)
{
// bit in the least significant position is 1
}
else
{
// bit in the least significant position is 0
}
decimal_to_hex(57);
should produce the output
00000039
void decimal_to_hex(int value)
{
// print the hexadecimal representation of value
}
unsigned int mask = 0x0000000F;
int hexvalue = value & mask;
if (hexvalue < 10)
{
// hexadecimal digit is hexvalue
}
else if(hexvalue == 10)
{
// hexadecimal digit is A
}
else if(hexvalue == 11)
{
// hexadecimal digit is B
}
and so on. If you wish to simplify your code, you may place all hexadecimal digits in an array of characters, and use the hexvalue as an index in the array as follows:
char hexdigit[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
unsigned int mask = 0x0000000F;
int hexvalue = value & mask;
// hexadecimal digit is hexdigit[hexvalue]
// print it with printf(“%c”, hexdigit[hexvalue]);
binary_to_decimal("00010101");
should produce the output value 21.
void binary_to_decimal(char value[])
{
// print the integer corresponding to the binary "value"
}
int i, digit;
int numdigits = strlen(value);
for (i = 0; i < numdigits; i++)
{
/* Extract binary digit from character */
digit = value[i] – '0';
printf("binary digit %d is: %d\n", i, digit);
}
systems/binary
directory create a text file named readme
(not readme.txt
, or README
, or Readme
, etc.)
that contains:
Turn in a printout copy of the readme
and binhex.c
files, and a sample output demonstrating how function works (one printout per team). If you work on the department Unix system, you will need to copy and paste your code into a text or a Word document in Windows, since you cannot print directly from Unix.
Leave the source code for all exercises in your directory systems/binary. Do not make any changes to these files after the due date for this assignment. If you wish to continue working on these exercises after the due date, make a copy of your directory binary using the following Unix command:
cp -r ~/systems/binary ~/systems/binary-copyA new directory called binary-copy will be created in your systems directory. You may now make any changes you want to the files in your binary-copy directory, at any time.