C 에서 overflow 된 연산 결과가 이상해요...

익명 사용자의 이미지

unix상에서 아래처럼 실행시켰는데요...

#include

int main() {
short s;

s=100000;
/* s=0x186A0; */
printf("s is [%d]\n",s);
return 0;
}

실제실행결과 -31702
저의예상결과 -7960

제생각에는 short가 2바이트니깐
100,000에 해당하는 0x186A0 이
앞에 1이 잘리고 0x86A0로 저장되는것같은데
음수이므로 2의보수로 읽으면 -7960 이 나와야하는데
실제로는 -31702가 나오네요.

문제가 뭔지, 확실히 아시는분 답변주심은 감사하겠습니다.

익명 사용자의 이미지

seeker wrote..
unix상에서 아래처럼 실행시켰는데요...

#include <stdio.h>

int main() {
short s;

s=100000;
/* s=0x186A0; */
printf("s is [%d]\n",s);
return 0;
}

실제실행결과 -31702
저의예상결과 -7960

제생각에는 short가 2바이트니깐
100,000에 해당하는 0x186A0 이
앞에 1이 잘리고 0x86A0로 저장되는것같은데
음수이므로 2의보수로 읽으면 -7960 이 나와야하는데
실제로는 -31702가 나오네요.

문제가 뭔지, 확실히 아시는분 답변주심은 감사하겠습니다.

0x86A0을 2진수로 바꾸면 1000011010100000 인데.

최상위비트가 1이므로 음수입니다..

따라서 님 말슴대로 보수를 취하면 111100101011111이 되고..

이걸 10진수로 바꿔보면 31071가 나옵니다..

+1시킨후 -부호를 붙여주면 음수가 되는거죠..

익명 사용자의 이미지

안녕하세요 ~ 결과는 -31072인 것 같습니다.

뭐 별로 재미도 없는 일인데 그냥 계산기로 계산해봤습니다.
말씀대로 앞에 1이 잘려나가죠.

그런데 그 뒤의 값을 잘못 생각하고 계신 것을 보면 2의 보수를 잘못 이
해하고 계신가 아닌가 생각합니다. ^^;

다음은 잘리고 남은 값입니다.
1000011010100000

그리고 그걸 2의 보수 취해주면 양수가 나오는데 부호만 다를 뿐 그 크기
는 같죠.

1000011010011111

-1을 해줍니다.

0111100101100000

31072죠. 그러므로 -31072가 맞는 것 같습니다. ^^;

그럼 즐거운 하루 되시기 바랍니다. ~

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.