[질문]함수 하나만 부탁드립니다..

leolo의 이미지

제가 함수 하나를 만들었는데요.. 너무 허접한 거 같아서
매크로로 간단히 정의 할 수 있는지 해서..

함수는
int getFlags(int flag, int idx)
flag의 idx 번째 값이 설정되었는지 확인하는 함수입니다.
리턴 값은 설정된 경우 1, 아니면 0을 리턴합니다.

기존의 것과 좀 다른 것은 다음과 같습니다.
예를 들어,
getFlags(100, 1); 이면 0을 리턴
getFlags(100, 3); 이면 1을 리턴합니다.
getFlags(100, 5); 이면 0을 리턴합니다.
즉, 눈에 보이는 숫자 그대로, 처음 부터 읽어, 리턴 값을 반환합니다.

이와 반대로 int setFlags(int flag, int idx) 라는 함수는
flag의 idx의 값을 설정합니다. 예를 들어,
setFlags(100, 1) 이라고, 하면 리턴 값은 101 이 됩니다.
만약, setFlags(100, 5) 라고 하면 리턴 값은 10100 입니다.

부탁드립니다.

제가 만든 것은 대충 이렇습니다.



// mflag 값 찾기 위해 사용.
static int getFlags(int flag, int idx, int itv)
{
    int j, div, ret;
    if(idx < itv)
        return -1;
    div = 1;
    for(j = 0; j < idx; j++)
        div *= 10;

    ret = 0;
    for(j = 0; j < itv; j++)
    {
        ret *= 10;
        ret += (flag % div) / (div/10);
        div /= 10;
    }
    return ret;
}
powerson의 이미지

음... 루프로 돌리는 것 보다는 비트 AND 연산 사용하시면 좀더 빠르지 않을까요?

------------------------------------------------------
아직은 젊다. 모든 것을 할 수 있는 나이란 말이지.

k2hyun의 이미지

음... 약간 원하시는 것과 다를 것 같은데요.

flag 라면 0x01, 0x02, 0x04, 0x08 등등을 이용하시는 방법을 추천해 드립니다.

#define getFlag(a, b) ( a & (1 << (b-1)) )
#define setFlag(a, b) ( a |= (1 << (b-1)) )

간단하죠?? ^^;

더 이상 없다.

댓글 달기

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