필터 지우기
필터 지우기

bitset error for uint64

조회 수: 1 (최근 30일)
Jason Palmer
Jason Palmer 2019년 6월 19일
댓글: Jason Palmer 2019년 6월 19일
There seems to be a problem with the bitset function for uint64. Here is a simple example:
>> d = 38124631952277504;
>> dec2bin(d)
ans =
'10000111011100100010011010011000101101010100000000000000'
>> dec2bin(d+1)
ans =
'10000111011100100010011010011000101101010100000000000000'
>> dec2bin(bitset(d,1,'uint64'))
ans =
'10000111011100100010011010011000101101010100000000000000'
>> dec2bin(bitset(d,2,'uint64'))
ans =
'10000111011100100010011010011000101101010100000000000000'
>> dec2bin(bitset(d,3,'uint64'))
ans =
'10000111011100100010011010011000101101010100000000000000'
>> dec2bin(bitset(d,4,'uint64'))
ans =
'10000111011100100010011010011000101101010100000000001000'
>> dec2bin(d+8)
ans =
'10000111011100100010011010011000101101010100000000001000'
So the bits only change successfully from 4th bit on, but fail to set fo the first three bits. This makes no sense that I can see and seems to be a serious error in Matlab.
Any help would be appreciated. Or advice on how to alert Matlab engineers.
Thanks,
Jason

채택된 답변

Utkarsh Belwal
Utkarsh Belwal 2019년 6월 19일
편집: Utkarsh Belwal 2019년 6월 19일
Read the documentation of dec2bin, it is written that if number is greater than flintmax then it might not work properly. In your case d is greater than flintmax.
  댓글 수: 2
Jason Palmer
Jason Palmer 2019년 6월 19일
Ok, thanks, should have read that. Just assumed it worked since it returns an answer.
Jason Palmer
Jason Palmer 2019년 6월 19일
Probably better to return error when d is greater than flintmax, since gambling on "may be correct" seems like it would never be an acceptable situation when you are dealing with bit setting.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 6월 19일
>> d = 38124631952277504;
>> dec2bin(d)
ans =
'10000111011100100010011010011000101101010100000000000000'
>> dec2bin(d+1)
ans =
'10000111011100100010011010011000101101010100000000000000'
d is a double precision array, and the distance from d to the next largest representable number is greater than 1. In fact, it's 8.
>> eps(d)
ans =
8
>> isequal(d, d+1) % returns true
If you want to work with integer data that's this large, I recommend working with integer data rather than double. Make your d variable an uint64 from the beginning. This will also allow you to see the change you're making with each bitset call without needing dec2bin.
>> du = uint64(38124631952277504)
du =
uint64
38124631952277504
>> du1 = bitset(du, 1)
du1 =
uint64
38124631952277505
>> du2 = bitset(du, 2)
du2 =
uint64
38124631952277506
>> du3 = bitset(du, 3)
du3 =
uint64
38124631952277508
>> du4 = bitset(du, 4)
du4 =
uint64
38124631952277512
If you do need to check the bits explicitly, use bitget. It's vectorized so you can get all the bits at once. If you need it as a char vector, you can combine bitget with sprintf.
bitget(du, 64:-1:1)
bitget(du1, 64:-1:1)
sprintf('%d', bitget(du4, 64:-1:1))
  댓글 수: 1
Jason Palmer
Jason Palmer 2019년 6월 19일
Ah, great, thanks for the explanation. Makes sense now. Cheers!

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by