단일 기호 비트 의 넘 침 과 수정 (컴퓨터 구성 원리)

#include
using namespace std;
/*
          
  :
		1)        
		2)      
		      :               
		 	a)   ,   
			b)        ,    
		       
			1)        ,                 -2^n 
			2)         ,               +  2 ^ n 

    4             -8 ~ +7    
*/
const int MAXN = 4;
int binary_x[MAXN], binary_y[MAXN]; 
int ans[MAXN];	//   
int carry[2];	// 0:     , 1:           
//      2  
void bToD (int x, int binary[]){
	if(x < 0) binary[0] = 1;
	else binary[0] = 0; 
	int cnt = MAXN - 1;
	x = abs(x);
	while(x) {
		binary[cnt--] = x % 2;
		x /= 2;
	}
} 
//     ,      
void oCTC(int binary[]) {
	if (binary[0] == 0) return ; 
	//         1, 1  (    1)            ,
	int flag = 0; //        1 
	for (int i = MAXN - 1; i > 0; --i) { //i !=0          
		if(flag == 0 && binary[i]){//        1,       1 
			flag = 1;
		} else if(flag == 1) {	//     1          
			binary[i] = !binary[i]; 
		} 
	} 	 
}
//       
void add(int ans[], int binary_x[], int binary_y[]) {
	int temp = 0;
	for(int i = MAXN - 1; i >= 0; --i) {
		if(i == 0) {	//             
			carry[1] = temp;
		} 
		if ( binary_x[i] && binary_y[i] ) {	 
			ans[i] = temp;
			temp = 1;
		} else if(binary_x[i] ^ binary_y[i] && temp){
			ans[i] = 0;
			temp = 1;
		} else if(binary_x[i] ^ binary_y[i] && temp == 0){
			ans[i] = 1;
			temp = 0;
		} else {
			ans[i] = temp;
			temp = 0;
		}
		
	}
	carry[0] = temp; //        
} 
//       10   
int getAns(int binary[]) {
	int result = 0; 
	for (int i = 1; i < MAXN; ++i) {
		if (binary[i]) {
			result += pow(2, MAXN - i - 1);	
		}
	}
	if(binary[0] && result == 0) {	//    
		result = -pow(2, MAXN - 1);
	} else if(binary[0]) result = -result;
	return result;
}
 //          
bool check(int a, int b) {
 	if (a < -8 || a > 7 || b < -8 || b > 7) {
 		return 1;
	} else {
		return 0;
	}
 } 
 void init() {
 	memset(binary_x, 0, sizeof(binary_x));
 	memset(binary_y, 0, sizeof(binary_y));
 	memset(ans, 0, sizeof(ans));
 	memset(carry, 0, sizeof(carry));
 }
int main (){
	while(1) {
		int a, b;
		cout << "       :" << endl;
		cin >> a >> b;
		init(); 
		//          
		if( check(a, b) ){
			cout << "    -8 ~ +7       ~" << endl; 
			continue;
		}
		// a   b          
		bToD(a, binary_x);
		bToD(b, binary_y);
		/*
		for (int i = 0; i < MAXN; ++i) cout << binary_x[i];
		cout << endl;
		 for (int i = 0; i < MAXN; ++i) cout << binary_y[i];
		cout << endl;
		*/ 
		//  a   b             
		oCTC(binary_x);
		oCTC(binary_y);
		cout << a << "    :" << endl; 
		for (int i = 0; i < MAXN; ++i) cout << binary_x[i];
		cout << endl;
		cout << b << "    :" << endl; 
		for (int i = 0; i < MAXN; ++i) cout << binary_y[i];
		cout << endl;
		
		//       +   
		add(ans, binary_x, binary_y); 
		/*
		for (int i = 0; i < MAXN; ++i) cout << ans[i];
		cout << endl;
		*/ 
		//      
		int mod = pow(2, MAXN);
		int result = 0;
		cout << "      :" << endl; 
		if ( carry[0] ^ carry[1] ) {  //       
			//        
			oCTC(ans);
			result = getAns(ans);
			if (carry[0]) {
				result -= mod;
				cout << result << endl;
			}
			else {//           
				result += mod;
				cout << result << endl;
			} 
		} else {	//              
			oCTC(ans);
			result = getAns(ans);
			cout << result << endl;
		} 
	} 
	return 0;
} 

좋은 웹페이지 즐겨찾기