long byte로 변환 시프트 횟수가 음수이거나 너무 큽니다. 정의되지 않은 동작입니다.

Lightstar의 이미지


자바클라이언트에서 시간(1970년 이후)을 long형으로 얻고 byte[8]로 바꿔서 c서버로 보내고 c서버에서 얻은값을 long으로 다시 바꾼 후 c에서도 시간을 얻어 비교하려고합니다.

자바

long time=new Date().getTime();
byte tim[]=new byte[8];
tim[0]=(byte) (time);
tim[1]=(byte) (time>>8);
tim[2]=(byte) (time>>16);
tim[3]=(byte) (time>>24);
tim[4]=(byte) (time>>32);
tim[5]=(byte) (time>>40);
tim[6]=(byte) (time>>48);
tim[7]=(byte) (time>>56);
m_sock_out_stream.write(tim);
m_sock_out_stream.flush();

서버
(byte형 p의 31~38인덱스에 들어옵니다.)
long time=(p[31]<<0)+(p[32]<<8)+(p[33]<<16)+(p[34]<<24)+(p[35]<<32)+(p[36]<<40)+(p[37]<<48)+(p[38]<<56);
long curTime=time(NULL);
printf("\n전송된 시간= %d\n현재 시간 = %d\n", timeStamp, curTime);


제가 int형 데이터를 딱 저런 방법으로 시프트해서 보내고 받고 있습니다. 그런데 long이라 뭐가 다른건지 다르게 나오네요.
int형에서는 보내줄 때 시프트 하기 전에 & 0xFF000000와 같은 연산을 하는데 long형에서는 어떻게 해야될지 모르겠습니다.
현재 결과값은
전송된 시간=-1832012775
현재 시간=1398327313
이렇게 나옵니다.

<<32부터 시프트 횟수가 음수이거나 너무 큽니다. 워닝이 나오는데 어떻게 해야 될까요?

익명 사용자의 이미지

서버 쪽에서 각각의 p[...] 앞에 (long)이라고 형변환 연산자를 넣어주면
경고가 안뜨겠죠.

그리고 쉬프트 연산자를 쓰시려면 unsigned 형이 더 낫지 않을까요?
signed형의 경우에는 쉬프트 시에 동작이 좀 까다롭습니다.

Lightstar의 이미지

답변 감사합니다.

댓글 달기

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