CSC373/406: C: Bit Operations [41/42] Previous pageContentsNext page

file:bitops.c [source]
00001: // shows the effect of various bit operations
00002: #include <stdio.h>
00003: 
00004: #include "show-bytes.c"
00005: int main() {
00006:   int x,y;
00007:   unsigned int z;
00008:   printf("Enter 2 numbers\n");
00009:   scanf("%x %x", &x, &y);
00010:   z = x;
00011:   printf("x in binary is ");
00012:   print_binary(x);
00013:   printf("\ny in binary is ");
00014:   print_binary(y);
00015:   printf("\n\nx & y        = ");
00016:   print_binary(x&y);
00017:   printf("\nx | y        = ");
00018:   print_binary(x|y);
00019:   printf("\nx ^ y        = ");
00020:   print_binary(x^y);
00021:   printf("\n\nx            = ");
00022:   print_binary(x);
00023:   printf("\n~x           = ");
00024:   print_binary(~x);
00025:   printf("\nx<<1         = ");
00026:   print_binary(x << 1);
00027:   printf("\nx<<2         = ");
00028:   print_binary(x << 2);
00029:   printf("\nx>>3         = ");
00030:   print_binary(x>>3);
00031:   printf("\n\nx in decimal    = %d\n", x);
00032:   printf("~x in decimal   = %d\n", ~x);
00033:   printf("x<<1 in decimal = %d\n", x<<1);
00034:   printf("x<<2 in decimal = %d\n", x<<2);
00035:   printf("x>>1 in decimal = %d\n", x>>1);
00036:   printf("\n");
00037:   printf("\nz            = ");
00038:   print_binary(z);
00039:   printf("\n~z           = ");
00040:   print_binary(~z);
00041:   printf("\nz<<1         = ");
00042:   print_binary(z << 1);
00043:   printf("\nz<<2         = ");
00044:   print_binary(z << 2);
00045:   printf("\nz>>3         = ");
00046:   print_binary(z>>3);
00047:   printf("\n\nz in decimal    = %u\n", z);
00048:   printf("~z in decimal   = %u\n", ~z);
00049:   printf("z<<1 in decimal = %u\n", z<<1);
00050:   printf("z<<2 in decimal = %u\n", z<<2);
00051:   printf("z>>1 in decimal = %u\n", z>>1);
00052:   printf("\n");
00053:   return 0;
00054: }
00055: 

Previous pageContentsNext page