• Articles
  • Auxiliarry Carry & Parity Detector
Published by
Jun 6, 2014

Auxiliarry Carry & Parity Detector

Score: 3.2/5 (17 votes)
*****
This program is useful to check if two numbers can generate an Auxiliary carry or not upon addition. This task can be cumbersome because C/C++ like programming languages although perform in binary in Machine level but their abstraction on binary level & operation on one or more than one variables on binary level to determine the properties of those numbers is not possible.

The above mentioned program can be very useful in engineering the idea to perform on the binary level in High level programming languages like C/C++. Here it is also mandatory to mention that this program is not compatible in C but the basic algorithm & implementation technique can remain same.

This is helpful for mostly those people who are interested in embedded programming or embedded software developing like me but it can still provide the idea/technique to work on the binary level. I created this program for my design of an Emulator for micro-controllers which is able to interpret between hex file & assembly source code to mimic the hardware operation on software level.

The main source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <iostream>
#include <bitset>
#include <string>
#include <sstream>
using namespace std;
bool AC;
bool P;
string dec2bin(int Decimal_Value){
	ostringstream os;
	os<<bitset<8>(Decimal_Value);
	return os.str();
}
void AC_Flag_Update(unsigned char a, unsigned char b){
	string s1 = dec2bin((int)a);
	string s2 = dec2bin((int)b);
	if ( s1.substr(4,1) == "1" && s2.substr(4,1) == "1" )
		AC = true ;
	else if ( s1.substr(4,1) == "0" && s2.substr(4,1) == "0" )
		AC = false;
	else
		AC_Flag_Update(a<<1 , b<<1);

}
void P_Flag_Update(unsigned char a){
	P = false;
	string s = dec2bin((int)a);
	for ( int i = 0; i < s.size(); i++ ){
		if( s.substr(i,1) == "1" )
			P =!P;
	}
}
int main()
{

	unsigned char c = 0x1a;
	unsigned char d = 0x06;
        AC_Flag_Update(c,d);
	P_Flag_Update(d);
	if(AC)
		cout<<"AC is present"<<endl;
	else
		cout<<"NO AC is present"<<endl;
	if(P)
		cout<<"P is SET"<<endl;
	else
		cout<<"P is CLEAR"<<endl;
}


And the Makefile:

1
2
3
4
5
6
7
8
9
10
11

TARGET= main
CC= g++ -std=c++11

all:
	$(CC) -Os -o $(TARGET) $(TARGET).cpp
run: $(TARGET)
	./$(TARGET)
clean:
	rm -f *~ $(TARGET)



AC is present
P is CLEAR