home Sandy, UT
Wednesday, 2017.11.22
04:17 MDT [GMT-7]
Brionews - Company Logo Condition: Partly Cloudy
Temperature: 4°C (39.2°F)
Barometer: 1029 mb and rising
IT
MIX
News
Low Cost Affordable C C++ PHP mySQL Perl Programs Development
Assignment for Discrete Math class, short for Discrete Structures.
Assignment was that given 2 binary strings, to perform the bitwise operations of AND, OR and XOR.
Download the executable: bitwise.exe
September 1, 2004

Bitwise Operations on Binary Strings

/**
  *  @title: bitwise.cpp
  *  @date: 2004.09.01 - 5:05 am
  *  @author: vinnie
  *  @ source for bitwise.exe
  */

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned char* myAnd( unsigned char *s1, unsigned char *s2)
{
unsigned static char res[32];
memset( res, 0x0000, 33);
memset( res, 0x0030, 32);

for ( int i = 0; i < 32; i++)
  if ( s1[i] == '1' && s2[i] == '1')
    res[i] = '1';

return res;
}

unsigned char* myOr( unsigned char *s1, unsigned char *s2)
{
unsigned static char res[32];
memset( res, 0x0000, 33);
memset( res, 0x0030, 32);

for ( int i = 0; i < 32; i++)
  if (    (s1[i] == '0' && s2[i] == '1')
       || (s1[i] == '1' && s2[i] == '0')
       || (s1[i] == '1' && s2[i] == '1')
     ) res[i] = '1';

return res;
}

unsigned char* myXor( unsigned char *s1, unsigned char *s2)
{
unsigned static char res[32];
memset( res, 0x0000, 33);
memset( res, 0x0030, 32);

for ( int i = 0; i < 32; i++)
  if (    (s1[i] == '0' && s2[i] == '1')
       || (s1[i] == '1' && s2[i] == '0')
     ) res[i] = '1';

return res;
}

void main( )
{
unsigned char ac1[33], ac2[33];
system("color 47");

BEGIN:
memset( ac1, 0x0000, 33);
memset( ac2, 0x0000, 33);
memset( ac1, 0x0030, 32);
memset( ac2, 0x0030, 32);

system("cls");
printf( " Max. 32 bits strings !!\n\n Insert the 1st number: ");
gets( (char*)ac1);

printf( "\n Insert the 2nd number: ");
gets( (char*) ac2);

int len1 = strlen( (const char*) ac1);
int len2 = strlen( (const char*) ac2);
int i=0;
for ( i = len1; i < 32; i++)
  ac1[i] = '0';
for ( i = len2; i < 32; i++)
  ac2[i] = '0';

unsigned char ac3[33], ac4[33];

memset( ac3, 0x0000, 33);
memset( ac4, 0x0000, 33);
memset( ac3, 0x0030, 32);
memset( ac4, 0x0030, 32);

int s = 0;
int d = 32 - len1;
for ( ; d < 32; d++, s++ )
  ac3[d] = ac1[s];

s = 0;
d = 32 - len2;
for ( ; d < 32; d++, s++ )
  ac4[d] = ac2[s];

gotoxy( 6, 7); printf(" %s", ac3);
gotoxy( 6, 8); printf(" %s", ac4);
//  ==============================
//  AND
unsigned char res[32];
memset( res, 0x0000, 33);
memset( res, 0x0030, 32);

unsigned char *p = myAnd( ac3, ac4);
for ( int i = 0; i < 32; i++)
  res[i] = *( p + i);

gotoxy( 3, 10); printf("AND %s", res);
//  =================================
//  OR
memset( res, 0x0000, 33);
memset( res, 0x0030, 32);

p = myOr( ac3, ac4);
for ( int i = 0; i < 32; i++)
  res[i] = *( p + i);

gotoxy( 4, 11); printf("OR %s", res);
//  =================================
//  XOR
memset( res, 0x0000, 33);
memset( res, 0x0030, 32);

p = myXor( ac3, ac4);
for ( i = 0; i < 32; i++)
  res[i] = *( p + i);

gotoxy( 3, 12); printf("XOR %s", res);
//  =================================
unsigned char answer;
printf("\n\n Another one? [Y/y/N/n] ");
answer = getchar();
if ( answer == 89 || answer == 121 )
  goto BEGIN;
system("color 0f");
}

2004.09.01

Vincenzo Maggio