3. Using the online compiler:-
• Enter the code from the file bitwise_demo.c
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
byte_t result;
byte_t second_byte;
byte_t first_byte;
int shift;
// Reading the first byte. Integer is entered then cast to byte_t.
printf("Enter the value of the first_byte (0-255): ");
scanf("%d", &i);
first_byte = (byte_t)i;
// Reading the second byte. Integer is entered then cast to byte_t.
printf("Enter the value of the second_byte (0-255): ");
scanf("%d", &i);
second_byte = (byte_t)i;
printf("\n\n");
// The results will be printed out in hexadecimal.
// The %X placeholder is for hexadecimal output.
// This can be extended to %02X, i.e. a hexadecimal
// character, 2 characters wide, with leading blanks
// filled-in as zeros.
// Bitwise inversion
result = ~first_byte;
printf("The value of ~(%02X) is %02X\n", first_byte, result);
result = ~second_byte;
printf("The value of ~(%02X) is %02X\n\n\n", second_byte, result);
// Bitwise AND, OR, XOR
result = first_byte & second_byte;
printf("The value of (%02X) & (%02X) is %02X\n",
first_byte, second_byte, result);
result = first_byte | second_byte;
printf("The value of (%02X) | (%02X) is %02X\n",
first_byte, second_byte, result);
result = first_byte ^ second_byte;
printf("The value of (%02X) ^ (%02X) is %02X\n",
first_byte, second_byte, result);
system("pause");
return 0;
}
• Add this source code to your logbook.
• Run the program. You will be prompted to enter two values between 0 and 255. Initially enter 234 and 37.
• Add the output of this program to your logbook.
• Repeat this with other values of your choice.
• Write a short reflective account of the code concentrating on its functionality and comparing the outputs against the source code.
#include <stdio.h>
int main(int argc, char *argv[]) {
byte_t result;
byte_t second_byte;
byte_t first_byte;
int shift;
printf("Enter the value of the first_byte (0-255): ");
scanf("%d", &i);
first_byte = (byte_t)i;
printf("Enter the value of the second_byte (0-255): ");
scanf("%d", &i);
second_byte = (byte_t)i;
printf("\n\n");
result = ~first_byte;
printf("The value of ~(%02X) is %02X\n", first_byte, result);
result = ~second_byte;
printf("The value of ~(%02X) is %02X\n\n\n", second_byte, result);
// Bitwise AND, OR, XOR
result = first_byte & second_byte;
printf("The value of (%02X) & (%02X) is %02X\n",
first_byte, second_byte, result);
result = first_byte | second_byte;
printf("The value of (%02X) | (%02X) is %02X\n",
first_byte, second_byte, result);
result = first_byte ^ second_byte;
printf("The value of (%02X) ^ (%02X) is %02X\n",
first_byte, second_byte, result);
system("pause");
return 0;
}
Comments
Leave a comment