bitshift in matlab vs ishft in fortran

조회 수: 1 (최근 30일)
Jaden
Jaden 2024년 9월 16일
답변: Steven Lord 2024년 9월 16일
Hi,
I am trying to convert a large 64 bit number into 2x 32 bit numbers, Here is my code:
U = bitshift(v, -24);
L = bitand(v, 0x000000ffffffs64);
This is replicated off of fortran code:
U = ishft(v, -24)
L = iand(v, Z'000000ffffff')
For value v = 2830037, the L's agree, but "bitshift(2830037, -24) = 0" and "ishft(2830037, -24) = 1".
I am confused. Any help would be appreciated!
  댓글 수: 1
Shashi Kiran
Shashi Kiran 2024년 9월 16일
Hi Jaden,
I see that U is zero when running the Fortran code(in onlinegdb compiler) as well. Are there any other ways to replicate the issue you are experiencing?

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2024년 9월 16일
Instead of using bit operations yourself, why not just use typecast?
t = 'int32';
x = randi([intmin(t), intmax(t)], 1, 2, t) % 2 random int32 values
x = 1×2
56777918 -1257821879
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = typecast(x, 'int64')
y = int64 -5402303834441491266
z = typecast(y, t)
z = 1×2
56777918 -1257821879
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
format hex
x
x = 1×2
03625cbe b5072949
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y
y = int64
b507294903625cbe
The swapbytes function may also be of interest to you.

카테고리

Help CenterFile Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by