// 1.5 Exercise 6.cpp : main project file.
#include "stdafx.h"
#include <stdio.h>
int sra(int x, int n);
int main()
{
int intInputNum;
int finalValue1;
int finalValue2;
printf("Please enter a integer and program will shift the integer 2 places to right,\n it will give value for both arithmetic as well as Logical shift\n");
scanf("%d",&intInputNum);
finalValue1 = intInputNum >> 2;
finalValue2 = sra(intInputNum, 2);
printf("Logical %d \n Arithmetic %d\n", finalValue1, finalValue2);
return 0;
}
int sra(int x, int k)
{
int xsrl = (unsigned)x >> k;
unsigned mask = k ? ((1 << (8 * sizeof(int)-k)) - 1) : 0;
return (x < 0) ? mask | xsrl : xsrl;
}
샘플 실행 : 음수 num 입력에서 산술 Shift가 작동하지 않습니까? 내가 여기서 뭘 잘못하고 있는거야?
Please enter a integer and program will shift the integer 2 places to right, it will give value for both arithmetic as well as Logical shift -500 Logical -125 Arithmetic 1073741823왜이 올바른 값을 표시되지?
부호있는 숫자를 이동하는 것은 좋은 생각이 아닙니다. –
아마도 :하지만 그건 위의 질문이 아니 었습니다. 문제는 "sra"가 작동하지 않는 이유입니다 (부호없는 숫자 만 사용함). –