0xffffffff >> 32 의 결과....

zelon의 이미지

갑자기 옆의 친구가 묻더군요...

#include <iostream>

using namespace std;

int main()
{
	int range = 32;
	int i = 0xffffffff >> range;

	cout << i << endl;
}


이 결과가 얼마일까요.... -_-;;

다음과 같이 테스트해보세요. >> 할 때 range 를 32 로도 해보시구요. 각각 다른 컴파일러에도 테스트해보세요.

제가 해본 바로는 값이 32, 0, -1, 가 나오는데, VC++ 6.0, .NET 2003, gcc

왜 값들이 다른걸까요... 어느 게 표준일까나요...

버려진의 이미지

저 변환식 자체가 비표준(?) 아닌가요?

너무 크다고 경고가 나올것 같은데..

lsj0713의 이미지

Quote:

If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

정의되지 않은 행동입니다.

Darkcircle의 이미지

zelon wrote:
갑자기 옆의 친구가 묻더군요...

#include <iostream>

using namespace std;

int main()
{
	int range = 32;
	int i = 0xffffffff >> range;

	cout << i << endl;
}


이 결과가 얼마일까요.... -_-;;

다음과 같이 테스트해보세요. >> 할 때 range 를 32 로도 해보시구요. 각각 다른 컴파일러에도 테스트해보세요.

제가 해본 바로는 값이 32, 0, -1, 가 나오는데, VC++ 6.0, .NET 2003, gcc

왜 값들이 다른걸까요... 어느 게 표준일까나요...

gcc가 아니라 g++이 맞는것 같습니다. :)
전 위 식대로 안하고 다음과 같이 했는데 이상하더군요 -_-;

(test.cpp)

#include <iostream>
using namespace std;
int main(void){
    int range=32;
    int i=0xffffffff;
    for(int j=1;j<=range;j++){
        if(j==1){
            cout << "    " << j << " : " << i;
        if(j>1){
            if(j<10){
                cout << "    " << j << " : " << (i=(i>>1));
            } else {
                cout << "   " << j << " : " << (i=(i>>1));
            }
        }
        if(j%4==0) cout << endl;
    }
}
:wq

#g++ -o test test.cpp

결과는 32번 모두 -1이 나왔습니다. -_-;

---------------------------------------------------------------
폐인이 되자 (/ㅂ/)

doldori의 이미지

lsj0713님이 인용하신 문구는 C 표준인가요?
C++ 표준은 좀 다르네요.

Quote:
5.8/3

The value of E1 >> E2 is E1 right-shifted E2 bit positions.
If E1 has an unsigned type or if E1 has a signed type and a nonnegative value,
the value of the result is the integral part of the quotient of E1
divided by the quantity 2 raised to the power of E2.
If E1 has a signed type and a negative value, the resulting value is
implementation-defined.


적어도 undefined는 아니로군요. :)
kslee80의 이미지

이 두 마디의 실제적인 의미는 비슷하지 않나요?

undefined 되어 있는 행동이더라도, 구현시 그 행동에 대해서 어떤 형태로든
구현하게 될 것이고, 그것을 implementation-defined 라고 말할수 있지 않을까
싶습니다.

doldori의 이미지

kslee80 wrote:
이 두 마디의 실제적인 의미는 비슷하지 않나요?

undefined 되어 있는 행동이더라도, 구현시 그 행동에 대해서 어떤 형태로든
구현하게 될 것이고, 그것을 implementation-defined 라고 말할수 있지 않을까
싶습니다.


아니오, 매우 다릅니다. C 또는 C++ 표준에서 얘기하는 undefined, implementation-defined,
unspecified의 의미는 다음 링크에 잘 나와 있습니다.

http://www.embedded.com/story/OEG20020321S0014

요약하면,
- undefined: 잘못된 프로그램으로 인하여 발생하는 결과. (예: null pointer를 dereference 하는 것)
- implementation-defined: 프로그램의 잘못이 아니라 각 구현에 따라 달라질 수 있는 사항.
각 구현체의 제작자는 이러한 사항을 문서화할 의무가 있음. (예: 1바이트를 구성하는 비트 수)
- unspecified: implementation-defined와 같은 성격을 가지지만 이를 문서화할 의무는 없음.
(예: 함수 인자의 평가 순서)

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.