Chapter 7. Bitwise Operators and Enumeration Types

Home

Bitwise operators

(uniary) bitwise complement ~
bitwise and &
bitwise exclusive or ^
bitwise inclusive or |
left shift <<
right shift >>

procedence and associativity of operators

Operators

Associativity

+(unary) -(unary) ++ -- !

* / %

+ -

<< >>

< <= > >=

== !=

&

^

|

&&

||

?:

= += -= *= /= <<= >>= etc

,(comma operator)

right to left

left to right

left to right

left to right

left to right

left to right

left to right

left to right

left to right

left to right

left to right

left to right

left to right

right to left

right to left

left to right

กก

PROGRAM 711 : main.c

#include <stdio.h>

void bit_print(int a);

int main(void)
{
  int k;

  for ( ; ; ) {
    printf("Input an integer: ");
    if (scanf("%d", &k) != 1)
      break;
    printf("\n%7d = ", k);
    bit_print(k);
    printf("\n\n");
  }
  printf("\nBye!\n\n");
  return 0;
}

กก

PROGRAM 712 : bit_print.c

/* Bit print an int expression. */

#include <limits.h>

void bit_print(int a)
{
  int i;
  int n = sizeof(int) * CHAR_BIT; /* in limits.h */
  int mask = 1 << (n - 1); /* mask = 100...0 */

  for (i = 1; i <= n; ++i) {
    putchar(((a & mask) == 0) ? '0' : '1');
    a <<= 1;
    if (i % CHAR_BIT == 0 && i < n)
      putchar(' ');
  }
}

PROGRAM 72 : pack.c

/* Pack 4 characters into an int. */

#include <limits.h>

int pack(char a, char b, char c, char d)
{
  int p = a; /* p will be packed with a, b, c, d */

  p = (p << CHAR_BIT) | b;
  p = (p << CHAR_BIT) | c;
  p = (p << CHAR_BIT) | d;
  return p;
}

PROGRAM 73 : unpack.c

/* Unpack a byte from an int. */

#include <limits.h>

char unpack(int p, int k) /* k = 0, 1, 2, or 3 */
{
  int n = k * CHAR_BIT; /* n = 0, 8, 16, or 24 */
  unsigned mask = 255; /* low-order byte */

  mask <<= n;
  return ((p & mask) >> n);
}

PROGRAM 74 : cr_emp_data.c

/* Create employee data in a short int. */

short create_employee_data(int id_no, int job_type, char gender)
{
  short employee = 0; /* start with all bits off */

  employee |= (gender == 'm' || gender == 'M') ? 0 : 1;
  employee |= job_type << 1;
  employee |= id_no << 7;
  return employee;
}

PROGRAM 751 : main.c

#include "p_r_s.h"

int main(void)
{
  int win_cnt = 0, lose_cnt = 0, tie_cnt = 0;
  outcome result;
  p_r_s player_choice, machine_choice;

  srand(time(NULL)); /* seed the random number generator */
  wrt_instructions();
  while ((player_choice = selection_by_player()) != quit)
  switch (player_choice) {
    case paper:
    case rock:
    case scissors:
      machine_choice = selection_by_machine();
      result = compare(player_choice, machine_choice);
      report_and_tabulate(result, &win_cnt, &lose_cnt,&tie_cnt);
    break;
  case game:
    wrt_game_status(win_cnt, lose_cnt, tie_cnt);
    break;
  case instructions:
    wrt_instructions();
    break;
  case help:
    wrt_help();
    break;
  default:
    printf("\nPROGRAMMER ERROR: Cannot get to here!\n\n");
    exit(1);
  }
  wrt_game_status(win_cnt, lose_cnt, tie_cnt);
  wrt_final_status(win_cnt, lose_cnt);
  return 0;
}

PROGRAM 752 : compare.c

#include "p_r_s.h"

outcome compare(p_r_s player_choice, p_r_s machine_choice)
{
  outcome result;

  if (player_choice == machine_choice)
    return tie;
  switch (player_choice) {
    case paper:
      result = (machine_choice == rock) ? win : lose;
      break;
    case rock:
      result = (machine_choice == scissors) ? win : lose;
      break;
    case scissors:
      result = (machine_choice == paper) ? win : lose;
      break;
    default:
      printf("\nPROGRAMMER ERROR: Unexpected choice!\n\n");
      exit(1);
  }
  return result;
}

PROGRAM 753 : p_r_s.h

/* The game of paper, rock, scissors. */

#include <ctype.h> /* for isspace() */
#include <stdio.h> /* for printf(), etc */
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */

enum p_r_s {paper,rock,scissors,game,help,instructions,quit};
enum outcome {win, lose, tie, error};

typedef enum p_r_s p_r_s;
typedef enum outcome outcome;

outcome compare(p_r_s player_choice,
p_r_s machine_choice);
void wrt_final_status(int win_cnt, int lose_cnt);
void wrt_game_status(int win_cnt, int lose_cnt, int tie_cnt);
void wrt_help(void);
void wrt_instructions(void);
void report_and_tabulate(outcome result,
int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr);
p_r_s selection_by_machine(void);
p_r_s selection_by_player(void);

PROGRAM 754 : report.c

#include "p_r_s.h"

void report_and_tabulate(outcome result,
int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr)
{
  switch (result) {
    case win:
      ++*win_cnt_ptr;
      printf("%27sYou win.\n", "");
      break;
    case lose:
      ++*lose_cnt_ptr;
      printf("%27sYou lose.\n", "");
      break;
    case tie:
      ++*tie_cnt_ptr;
      printf("%27sA tie.\n", "");
      break;
    default:
      printf("\nPROGRAMMER ERROR: Unexpected result!\n\n");
      exit(1);
  }
}

PROGRAM 755 : selection.c

#include "p_r_s.h"

p_r_s selection_by_machine(void)
{
  return ((p_r_s) (rand() % 3));
}

p_r_s selection_by_player(void)
{
  char c;
  p_r_s player_choice;

  printf("Input p, r, or s: ");
  while (isspace(c = getchar())) /* skip white space */
    ;
  switch (c) {
    case 'p':
      player_choice = paper;
      break;
    case 'r':
      player_choice = rock;
      break;
    case 's':
      player_choice = scissors;
      break;
    case 'g':
      player_choice = game;
      break;
    case 'i':
      player_choice = instructions;
      break;
    case 'q':
      player_choice = quit;
      break;
    default:
      player_choice = help;
      break;
  }
  return player_choice;
}

PROGRAM 756 : wrt.c

#include "p_r_s.h"

void wrt_final_status(int win_cnt, int lose_cnt)
{
  if (win_cnt > lose_cnt)
    printf("CONGRATULATIONS - You won!\n\n");
  else if (win_cnt == lose_cnt)
    printf("A DRAW - You tied!\n\n");
  else
    printf("SORRY - You lost!\n\n");
}

void wrt_game_status(int win_cnt, int lose_cnt, int tie_cnt)
{
  printf("\n%s\n%s%4d\n%s%4d\n%s%4d\n%s%4d\n\n",
    "GAME STATUS:",
    " Win: ", win_cnt,
    " Lose: ", lose_cnt,
    " Tie: ", tie_cnt,
    " Total: ", win_cnt + lose_cnt + tie_cnt);
}

void wrt_help(void)
{
  printf("\n%s\n",
    "The following characters can be used for input:\n"
    " p for paper\n"
    " r for rock\n"
    " s for scissors\n"
    " g print the game status\n"
    " h help, print this list\n"
    " i reprint the instructions\n"
    " q quit this game\n");
}

void wrt_instructions(void)
{
  printf("\n%s\n",
    "PAPER, ROCK, SCISSORS:\n"
    " In this game p is for \"paper,\" r is for \"rock,\" and"
    " s is for \"scissors.\"\n"
    " Both the player and the machine\n"
    " will choose one of p, r, or s."
    " If the two choices are the same,\n"
    " then the game is a tie. Otherwise:\n"
    " \"paper covers the rock\" (a win for paper),\n"
    " \"rock breaks the scissors\" (a win for rock),\n"
    " \"scissors cut the paper\" (a win for scissors).\n"
    "\n"
    " There are other allowable inputs:\n"
    " g for game status (the number of wins so far),\n"
    " h for help,\n"
    " i for instructions (reprint these instructions),\n"
    " q for quit (to quit the game).\n"
    "\n"
    " This game is played repeatedly until q is entered.\n"
    "\n"
    " Good luck!\n");
}

กก


[Last Update: 2001.5.24] Dongseo University Cyber Campus