首页
社区
课程
招聘
An analysis of XOR in two's complement.
发表于: 2009-10-22 13:29 2025

An analysis of XOR in two's complement.

2009-10-22 13:29
2025
新增 Lemma 6.
當 A, B 兩數均為偶數時,A 與 B之差若為 4 的倍數,則 (A⊕B)= (-A⊕-B).
例如:
(62,66) 相差 4.
(62,70) 相差 8.
(62,74) 相差 12.
(62,78) 相差 16.

這些 pairs 不整除 4 也不整除8。
(2,6) 這兩整數也是相差 4。


// test2.c : Algorithm design by Rock on April 1 2009.
// test2.c : First coding edition by Nudtsong on Oct 21 2009. (coding)
// test2.c : First revised edition by Ivanov on Oct 21 2009. (Modified Lemma 5)
// test2.c : Second revised by Ivanov on Oct 21 2009. (Added Lemma 6)

#include "stdio.h"

int main(int argc, char argv[])
{
	unsigned int a = 0;
	unsigned int b = 0;
	unsigned int c1 = 0;
	unsigned int c2 = 0;

LABEL_INPUT:
	// User inputÈë
	printf("Please input two integers: A, B must be odd or even\n");
	printf("A=");
	scanf("%d", &a);
	printf("B=");
	scanf("%d", &b);

	// Does check A,B one odd and one even number?
	if (((0 == (a % 2)) && (0 != (b % 2)))
		|| ((0 != (a % 2)) && (0 == (b % 2))))
	{
		printf("error:A, B must be odd or even numbers\n");
		goto LABEL_INPUT;
	}
	else if ((0 != (a % 2)) && (0 != (b % 2)))	// If A,B are odd numbes
	{
		printf("A, B are odd numbers\n");
		c1 = a ^ b;
		c2 = (-a) ^ (-b);
		printf("C1=%d, C2=%d\n", c1, c2);
	}
      
    else	// If A, B are even numbers
    {
        //printf("A, B are even numbers\n");

        if ((0 == (a % 4)) && (0 == (b % 4))) {

            if ((0 != (a % 8)) && (0 != (b % 8)))
            {
                // A,B are divisible by 4 rather than 8
                printf("A, B be divided 4 and be not divided 8\n");
                c1 = a ^ b;
                c2 = (-a) ^ (-b);
                printf("C1=%d, C2=%d\n", c1, c2);
            }
            else {
                printf("a or b is divisible by 8\n");
                goto LABEL_INPUT;
            }
        }
        else {
            if ((a % 4) == (b % 4)) {
                printf("A,B are even and 4 | abs(A-B) \n");
                c1 = a ^ b;
                c2 = (-a) ^ (-b);
                printf("C1=%d, C2=%d\n", c1, c2);
            }
            else
            {
                printf(" A,B are even but A or B or abs(A-B) is not divisible by 4\n");
                goto LABEL_INPUT;
            }

        }
            
    }
	
	system("pause");
    goto LABEL_INPUT;
	return 0;
}


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 2096
活跃值: (100)
能力值: (RANK:420 )
在线值:
发帖
回帖
粉丝
2






上传的附件:
2009-10-29 20:37
0
游客
登录 | 注册 方可回帖
返回
//