2진수 올림

익명 사용자의 이미지

최상위 비트에서 2진수 올림을 간단히 하는 방법이 없을까요?

예를들면 7544 (0x1D78) 값이 있으면 8192 (0x2000) 으로,
432987 (0x69B5B) 값이면 524288 (0x80000) 으로의 변환을
간!단!히 하는 방법이 없을까요?

snowall의 이미지

일단 질문에 물어보신건 16진수인데요...

피할 수 있을때 즐겨라! http://melotopia.net/b

익명 사용자의 이미지

아~ 표시를 16진수로 했습니다. 2진수로 하면 너무 길어서~ ㅎ

익명 사용자의 이미지

간!단!히! 하는 방법은 어셈블리 비트연산을 바로 하는 방법 밖에 모르겠네요.
C로 하면 대충

ceil = 1;
while (n != 0) {
n &= n-1;
ceil <<= 1;
}

이렇게 되지 않을까 싶은데, 진짜 돌아가는지는 확인해보세요.

익명 사용자의 이미지

막 돌려보니까 코드가 틀렸네요. 뻘쭘...

lehako의 이미지

답변 감사합니다. ^^

planetarium의 이미지

#include <stdio.h>
 
int main()
{
	int t = 7544;
	int u = 432987;
	int n = 2;
	int m;
 
	// version 1
	while (t)
	{
		t >>= 1;
		n <<= 1;
	}
 
	// version 2
	for (m=2; u>>=1; m<<=1) ;
 
	printf("%d %d\n", n, m);
 
	return 0;
}

vim에 있는 TOhtml 기능 한번 써볼랬는데 흰 바탕이라 그런지 영 안이쁘네요... ㅠ
다만 입력값이 0인 경우엔 따로 처리해줘야 할거에요.

lehako의 이미지

답변 감사합니다. ^^

lehako의 이미지

답변 달아주셔서 감사합니다.

위 부분을 system-c로 구현하여 logic으로 설계하려 합니다.
그래서 "loop를 사용 안하고 하는 방법이 혹시(?) 있지 않을까?" 하여 문의드렸습니다.
없을까요? ㅎ~

다시한번 답변 감사합니다.

Jane의 이미지

system-c에 적합한지는 제가 잘 모르겠습니다만,
loop를 사용안하시려면,
음..결국 0x2000이럴때만 아니면 최대비트보다 한비트 올리면 되니까..

if ((n&(n-1)) != 0)
n = 1 << (int)(log2(n));

로 하면 가능하지 않을까요?
log2에 오차에 따라 조금 결과가 달라질 수도 있겠네요..ㅠ.ㅠ

-------------------------
최선을 생각합니다.

lehako의 이미지

답변 감사합니다. log를 써야 하네요. ㅎ~

planetarium의 이미지

아... 그러네요. 올릴 필요가 없는 경우를 따로 생각했어야 하는데...

익명 사용자의 이미지

http://stackoverflow.com/questions/53161/find-the-highest-order-bit-in-c

조금 다르지만 비슷하게 응용 가능하겠습니다..

댓글 달기

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