Ch. 2 -- Bits, Bytes, Masks, etc.
Download
Report
Transcript Ch. 2 -- Bits, Bytes, Masks, etc.
Chapter 2
ASCII
/* This program shows the dual interpretations of char and
** unsigned char data types. (pg 54) */
#include <stdio.h>
main()
{
char a;
unsigned char b;
a='A';
b='B';
printf("%c %c %d %d\n",a,b,a,b);
a=183;
b=255;
printf("%d %d\n",a,b);
}
sizeof() operator
/* This program demonstrates the sizeof() operator. (pg 57) */
#include <stdio.h>
main()
{
int i;
char c;
double d;
printf("%d %d %d %d\n",sizeof(i),sizeof(c),sizeof(d),sizeof(float));
}
Bitwise NOT
/* This program demonstrates the bitwise not operator. (pg 59) */
#include <stdio.h>
main()
{
unsigned char a;
a=17;
a=~a;
printf("%d\n",a);
}
Bitwise AND
/* This program demonstrates the bitwise and operator. (pg 60) */
#include <stdio.h>
main()
{
unsigned char a,b;
a=17;
b=22;
a=a & b;
printf("%d\n",a);
}
Bitwise OR
/* This program demonstrates the bitwise or operator. (pg 60) */
#include <stdio.h>
main()
{
unsigned char a,b;
a=17;
b=22;
a=a | b;
printf("%d\n",a);
}
Bit operators (variables and constants)
/* This program demonstrates using the bitwise operators
** with variables and constants. (pg 61) */
#include <stdio.h>
main()
{
char x,y;
x=7;
y=6;
x=x&y;
y=x|16;
printf("%d %d\n",x,y);
}
Left/Right shift
/* This program demonstrates the bitwise shift operators,
** left-shift and right-shift. (pg 61) */
#include <stdio.h>
main()
{
unsigned char a,b;
a=17;
a=a << 2;
b=64;
b=b >> 3;
printf("%d %d\n",a,b);
}
Bitwise right-shift (negative)
/* This program demonstrates right-shifting a negative integer
** so that the shifted-in bits are 1 instead of 0 (pg 62) */
#include <stdio.h>
main()
{
char a,b;
a=17;
a=a >> 2;
b=-65;
b=b >> 2;
printf("%d %d\n",a,b);
}
Bitmasks
/* This program demonstrates setting a bit, clearing a bit, and
** reading a bit. (pg 64) */
#include <stdio.h>
main()
{
char a;
int i;
a=17;
a=a | (1 << 3); /* set 3rd bit */
printf("%d\n",a);
a=a & (~(1<<4)); /* clear 4th bit */
printf("%d\n",a);
for (i=7; i>=0; i--)
printf("%d ",(a&(1<<i)) >> i); /* read i'th bit */
printf("\n");
}
Memory map 1
/* Draw the memory map for this code (pg 65) */
#include <stdio.h>
main()
{
char a,b,c;
a=7;
b=-13;
c=0;
}
Memory map 2
/* Draw the memory map for this code (pg 65) */
#include <stdio.h>
main()
{
char a;
int b;
float c;
double d;
a=7;
b=-13;
c=0.1;
d=42.5;
}
Memory map 3
/* Draw the memory map for this code. Showing the bit patterns
** emphasizes the differences in storage of "6" (pg 66) */
#include <stdio.h>
main()
{
char a;
short int b;
char c;
a=6;
b=13;
c='6';
}
Memory map 4
/* Draw the memory map for this code. It can emphasize the
** value of a loop counter after the loop is done (pg 67) */
#include <stdio.h>
main()
{
int i,n;
n=0;
for (i=1; i<=4; i++)
n=n+i;
}
Memory map 5
/* Draw the memory map for this code. It can emphasize an
** unitialized variable (pgs 67-68) */
#include <stdio.h>
main()
{
int i,sum;
printf("%d\n",sum);
for (i=1; i<=10; i++)
if (i%2 == 0)
sum=sum+i;
printf("%d\n",sum);
}
Problem 1
/* Code for problem 1 - draw the memory map (pg 68) */
#include <stdio.h>
main()
{
char c=35;
char d='G';
int x=-42;
float f=17.25;
int i=1099563008;
double a=17.25;
}
Problem 9
/* Code for problem #9 in chapter 2 (pg 70) */
#include <stdio.h>
main()
{
unsigned char x,y,z;
x=15;
y=35;
z=133;
x=x|64;
y=y&3;
z=~z;
printf("%d %d %d\n",x,y,z);
}
Problem 10
/* Code for problem #10 in chapter 2 (pg 70) */
#include <stdio.h>
main()
{
int x=7;
x=(x|16)<<1;
printf("%d\n",x);
}
Problem 11
/* Code for problem #11 in chapter 2 (pg 70) */
#include <stdio.h>
main()
{
char i;
double d;
int t;
t=0;
for (i='z'; i>='w'; i--)
for (d=1.0; d<=1.5; d+=0.1)
if (d-1.3 > 0)
t++;
printf("%d\n",t);
}
Problem 12
/* Code for problem #12 in chapter 2 (pg 70) */
#include <stdio.h>
main()
{
int i,j;
j=0;
for (i=1; i<100; i=i<<1)
{
if (i % 5 > 1)
j=j | i;
printf("%d %d\n",i,j);
}
}
Problem 13
/* Code for problem #13 in chapter 2 (pg 71) */
#include <stdio.h>
main()
{
int i,j,k;
j=0;
k=32;
for (i=100; i>0; i-=10)
{
if (k/4 > 0)
j=j | k;
if (i <= 70 && i >= 40)
k=k<<1;
else
k=k>>1;
printf("%d %d %d\n",i,j,k);
}
}
Problem 17
/* This is the code for problem #17 in chapter 2 (pg 72) */
#include <stdio.h>
main()
{
int i;
double d;
char s[10];
s[0]='f'; s[1]='r'; s[2]='o'; s[3]='g';
d=0.0;
for (i=0; i<4; i++)
d=d+(double)(s[i]-'a');
}