Code: Select all
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
typedef unsigned int uint;
class HalfAdder;
class FullAdder;
uint bin_array_error = 0; // global variable to signal an overflow in the binary representation
/***********************************************************************
* Note that at the location where the logic functions are called
* (or practically anywhere in the code which use uint -> technically
* int 32 bit integer numbers to store one bit value)
* it should be ensured that uint type input variables are either
* 0x00000000 or 0x00000001 only because this is what has been assumed
* in the functions
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* lower bit is stored in lower array index: Littel Endian
* var[3] = {x, y, z} ==> x=> LSB and z => MSB
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* a uint array here is (generally) an array of unsigned integers, where each
* array element is made up of 32 bits, considered by the compiler as an unsigned integer
* and used to store only one bit of a (multibit) binary number
/***********************************************************************/
/************** Functions needed for programming block **************
*********************************************************************/
/*** This function returns the numerical value of the bits represented in the uint array ***/
uint value_of_reg(uint *reg, uint no_of_bits){ // Note that no_of_bits can be at maximum 31
uint i, j, return_val = 0, pow_of_2 = 1;
for(i = 0; i < no_of_bits; i++){
if(reg[i] != 0){
pow_of_2 = 1;
for(j = 0; j < i; j++)
pow_of_2 = pow_of_2 * 2;
return_val = return_val + pow_of_2;
// cout<<"\t\tInside function value_of_reg: \n"<<"\t\ti = "<<i<<"\treturn_val = "<<return_val<<endl;
}
}
return return_val;
}
/*** this function increments the bit representation by specified number of bits ***/
uint inc_bin_num(uint *reg, uint no_of_bits, uint no_of_inc){
uint i, carry = 0;
for(;no_of_inc > 0; no_of_inc--){
reg[0] = reg[0] + 1;
if(reg[0]%2 == 0){ // after doing reg[0]+1, reg[0]%2 will be zero only if reg[0] was previously 1, that is, value of reg[0] now is 2
reg[0] = reg[0]%2; // this will change value of reg[0] to 0
carry = 1; // and generate a carry
}
for(i=1;i<no_of_bits;i++){
if(carry == 1){
reg[i] = reg[i] + 1; // if there was a carry from the lower bit, increment the next higher bit
if(reg[i]%2 == 0) { // if this higher bit became 2 after addition ( %2 == 0); note taht after addition if the bit becomes 1 then we do not need to do anything
if(i==no_of_bits-1) {carry = 1; bin_array_error = 1; reg[i] = reg[i]%2; return bin_array_error;} // another carry should be generated; again, if this higher bit is the highest bit in the binary number, then an overflow is returned and the function terminates immediately
reg[i] = reg[i] % 2; // if it's not the highest bit, convert it to 0 (note that this lline never converts it to 1, as code execution will reach here only if the higher bit is 0 after addition
carry = 1;
}
else {carry = 0; break;}
}
}
}
}
/*********************************************************************
************** Functions needed for programming block ENDS HERE ****************/
/************** Logic function implementation block **************
*********************************************************************/
/*** Two input and gate ***/
uint and_2_inp(uint ip1, uint ip2){
return(ip1 & ip2);
}
/*** Two input or gate ***/
uint or_2_inp(uint ip1, uint ip2){
return(ip1 | ip2);
}
/*** 1 bit inverter (not gate) ***/
uint not_gate(uint ip1){
ip1 = ~ip1;
ip1 = ip1 - 0xfffffffe; // assuming that all the uint type input variables are either 0x00000000 or 0x00000001 only
return(ip1);
}
/*** Two input xor gate ***/
uint xor_2_inp(uint ip1, uint ip2){
return(ip1^ip2);
}
uint inc_bin_num_using_HA(uint *reg, uint no_of_bits, uint no_of_inc){
/**** This implementation seems to be a type of carry propagation adder
See the type that I intend to implement in materials\HA_array.JPG
Note that this function uses the same register as source and destination
that is, updates a registers value to store the incremented version
***/
// HalfAdder *HA_array;
HalfAdder HA_array;
uint i;
// HA_array = (HalfAdder *)calloc(no_of_bits, sizeof (HAlfAdder) );
HA_array = new HalfAdder [no_of_bits];
// HA_array = (HalfAdder*) calloc(no_of_bits, sizeof(HalfAdder) );
for(i=0; i< no_of_bits; i++){
if(i=0) {HA_array[i].compute(reg[i], 1); reg[i] = HA_array[i].get_sum();} // to the LSB of the binary number add 1 (always 1 is added to the LSB, obvious)
else{
HA_array[i].compute( reg[i], HA_array[i-1].get_carry_out() );
reg[i] = HA_array[i].get_sum();
}
}
return( HA_array.get_carry_out() ); // funtion returns the carry out from the last halfadder (but I think it may be better if it returned something else)
//delete [] HA_array;
}
/*********************************************************************
************** Logic function implementation block ENDS HERE **************/
class HalfAdder{
private:
uint sum, carry_out, ha_inp1, ha_inp2;
public:
HalfAdder():sum(0), carry_out(0), ha_inp1(0), ha_inp2(0) {}
HalfAdder(uint x, uint y){
ha_inp1 = x;
ha_inp2 = y;
}
void compute(uint i1, uint i2){ // Note: Constructor sets the values of the three input bits (fa_inp1, fa_inp2, carry_in) during object creation; This function sets the value as well as calculates Full Adder function when called
ha_inp1 = i1;
ha_inp2 = i2;
sum = xor_2_inp(ha_inp1, ha_inp2);
carry_out = and_2_inp(ha_inp1, ha_inp2);
}
uint get_sum(){
return sum;
}
uint get_carry_out(){
return carry_out;
}
void show_state(){
cout<<"\ninput 1 => "<<ha_inp1<<"input 2 => "<<ha_inp2<<endl;
cout<<"\n\tsum => "<<sum<<"\n\tcarry_out => "<<carry_out<<endl;
}
};
class FullAdder{
private:
uint sum, carry_out, fa_inp1, fa_inp2, carry_in;
public:
FullAdder():sum(0), carry_out(0), fa_inp1(0), fa_inp2(0), carry_in(0) {}
FullAdder(uint x, uint y, uint z){
fa_inp1 = x;
fa_inp2 = y;
carry_in = z;
}
void compute(uint i1, uint i2, uint c){ // Note: Constructor sets the values of the three input bits (fa_inp1, fa_inp2, carry_in) during object creation; This function sets the value as well as calculates Full Adder function when called
fa_inp1 = i1;
fa_inp2 = i2;
carry_in = c;
HalfAdder sec1(fa_inp1, fa_inp2), sec2;
sec1.compute(fa_inp1, fa_inp2);
sec2.compute( carry_in, sec1.get_sum() );
sum = sec2.get_sum(); // the sum output of the full adder is the sum output of section 2 half adder; see materials folder --> fulladder.gif
carry_out = or_2_inp( sec1.get_carry_out(), sec2.get_carry_out() );
}
uint get_sum(){
return sum;
}
uint get_carry_out(){
return carry_out;
}
void show_state(){
cout<<"\ninput 1 => "<<fa_inp1<<"\ninput 2 => "<<fa_inp2<<"\ncarry_in => "<<carry_in<<endl;
cout<<"\n\tsum => "<<sum<<"\n\tcarry_out => "<<carry_out<<endl;
}
};
int main(){
FullAdder obj;
uint reg_3_bit[3]={0,0,0}; // LSB...MSB
while( /* value_of_reg( reg_3_bit, 3)<8*/ bin_array_error ==0 ){
// cout<<"Current value of reg = "<<value_of_reg( reg_3_bit, 3)<<" "<<reg_3_bit[2]<<" "<<reg_3_bit[1]<<" "<<reg_3_bit[0]<<endl;
obj.compute(reg_3_bit[0], reg_3_bit[1], reg_3_bit[2]);
obj.show_state();
cout<<endl;
// inc_bin_num(reg_3_bit, 3, 1);
inc_bin_num_using_HA(reg_3_bit,3,1);
getch();
}
}
Code: Select all
C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\backward\backward_warning.h|32|warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.|
D:\CB_Files\bit_level_manip\code.cpp||In function `uint inc_bin_num_using_HA(uint*, uint, uint)':|
D:\CB_Files\bit_level_manip\code.cpp|108|error: aggregate `HalfAdder HA_array' has incomplete type and cannot be defined|
D:\CB_Files\bit_level_manip\code.cpp|111|error: invalid use of undefined type `struct HalfAdder'|
D:\CB_Files\bit_level_manip\code.cpp|7|error: forward declaration of `struct HalfAdder'|
||=== Build finished: 3 errors, 1 warnings ===|
What is the mistake here?