BASIC4MCU | C언어 | C언어 | signed형으로 선언을 한 후 음수값을 우측 시프트 하면 계속 음수로 만듭니다.
페이지 정보
작성자 키트 작성일2017-09-12 13:21 조회1,271회 댓글0건본문
signed형으로 선언을 한 후 음수값을 우측 시프트 하면 계속 음수로 만듭니다.unsigned 형으로 선언 하셔야 합니다.//#include <avr/io.h>//int a,b;//int main(){DDRA=0xff; DDRB=0xff;while(1){a=0x8000;for(b=0;b<15;b++){PORTA=~(a&0xff);PORTB=~(a>>8);a>>=0x0001;}}}a에 초기값 0b1000000000000000을 주고 1씩 우측 시프트 시키면0b10000000000000000b01000000000000000b0010000000000000이렇게 시프트 되는 것이 아니라0b10000000000000000b11000000000000000b1110000000000000이렇게 부호 비트가 1로 채워집니다.//avrstudio로 간단하게 테스트 코드를 만들어서 디버깅 돌리고어셈블리어로 봤습니다.<signed int로 선언 했을 시>12: a>>=0x0001;+00000112: 91800102 LDS R24,0x0102 Load direct from data space+00000114: 91900103 LDS R25,0x0103 Load direct from data space+00000116: 9595 ASR R25 Arithmetic shift right+00000117: 9587 ROR R24 Rotate right through carry+00000118: 93900103 STS 0x0103,R25 Store direct to data space+0000011A: 93800102 STS 0x0102,R24 Store direct to data space<unsigned int로 선언 했을 시>12: a>>=0x0001;+00000111: 91800102 LDS R24,0x0102 Load direct from data space+00000113: 91900103 LDS R25,0x0103 Load direct from data space+00000115: 9596 LSR R25 Logical shift right+00000116: 9587 ROR R24 Rotate right through carry+00000117: 93900103 STS 0x0103,R25 Store direct to data space+00000119: 93800102 STS 0x0102,R24 Store direct to data space
댓글 0
조회수 1,271등록된 댓글이 없습니다.