M
Morlaath
O.K. I compiled and ran the following code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef char* bebop;
typedef unsigned char* byte_pointer;
void show_bytes(byte_pointer start, int len)
{
int i;
for(i = 0; i < len; i++)
printf(" %.2X ", start);
printf("\n");
}
void show_bits(byte_pointer start, int len)
{
int i, j;
for(i = 0; i < len; i++)
{
for(j = 0; j < 8; j++)
{
if(j % 4 == 0)
printf(" ");
if((start << j) & (0x80))
printf("1");
else
printf("0");
}
}
printf("\n");
}
int multiply(int x, int m) {
if(m == 5)
return (x << 2) + x;
else if(m == 9)
return (x << 3) + x;
else if(m == 14)
return (x << 4) - (x << 1);
else if(m == -56) {
return -((x << 6) - (x << 3));
} else printf("Bad input\n");
}
int main() {
int x;
printf("Enter an int: ");
scanf("%d", &x);
printf("%d * 5 = %d\n", x, multiply(x, 5));
printf("%d * 9 = %d\n", x, multiply(x, 9));
printf("%d * 14 = %d\n", x, multiply(x, 14));
printf("%d * -56 = %d\n", x, multiply(x, -56));
bebop bb;
int bitch = 0x00000000000000000000000000000001;
int bob = 1;
printf("BYTES for bitch: ");
show_bytes((byte_pointer)&bitch, sizeof(bitch));
printf("bebop: %.2x\n", *bb);
}
I got a seg fault on this. Here is the output:
billybob@~/homeforbillybob ./problem4
Enter an int: 5
5 * 5 = 25
5 * 9 = 45
5 * 14 = 70
5 * -56 = -280
BYTES for bitch: 01 00 00 00
Segmentation fault (core dumped)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef char* bebop;
typedef unsigned char* byte_pointer;
void show_bytes(byte_pointer start, int len)
{
int i;
for(i = 0; i < len; i++)
printf(" %.2X ", start);
printf("\n");
}
void show_bits(byte_pointer start, int len)
{
int i, j;
for(i = 0; i < len; i++)
{
for(j = 0; j < 8; j++)
{
if(j % 4 == 0)
printf(" ");
if((start << j) & (0x80))
printf("1");
else
printf("0");
}
}
printf("\n");
}
int multiply(int x, int m) {
if(m == 5)
return (x << 2) + x;
else if(m == 9)
return (x << 3) + x;
else if(m == 14)
return (x << 4) - (x << 1);
else if(m == -56) {
return -((x << 6) - (x << 3));
} else printf("Bad input\n");
}
int main() {
int x;
printf("Enter an int: ");
scanf("%d", &x);
printf("%d * 5 = %d\n", x, multiply(x, 5));
printf("%d * 9 = %d\n", x, multiply(x, 9));
printf("%d * 14 = %d\n", x, multiply(x, 14));
printf("%d * -56 = %d\n", x, multiply(x, -56));
bebop bb;
int bitch = 0x00000000000000000000000000000001;
int bob = 1;
printf("BYTES for bitch: ");
show_bytes((byte_pointer)&bitch, sizeof(bitch));
printf("bebop: %.2x\n", *bb);
}
I got a seg fault on this. Here is the output:
billybob@~/homeforbillybob ./problem4
Enter an int: 5
5 * 5 = 25
5 * 9 = 45
5 * 14 = 70
5 * -56 = -280
BYTES for bitch: 01 00 00 00
Segmentation fault (core dumped)