Get bits of number.

조회 수: 39 (최근 30일)
Raldi
Raldi 2015년 4월 13일
편집: Guillaume 2015년 4월 13일
I have a 16 bit double I got from using wavread that I want to get the first 8 bits from.
As an example lets say I have 67. Inside my computer it must be represented as a series of bits, 0111001 in this case. So lets say that I just want to have the last for of them 1001.
How can this be done?
  댓글 수: 3
Guillaume
Guillaume 2015년 4월 13일
Also, 67 in binary is not 00111001:
>>dec2bin(67, 8)
ans = 0100011
Raldi
Raldi 2015년 4월 13일
I probably meant 57. Also yeah they are double but I calculated that I only need 16 bits to represent all numbers in that range, all the rest are zeros.

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

채택된 답변

Guillaume
Guillaume 2015년 4월 13일
Assuming you have a 16 bit integer, the simplest way to extract some bits from it is to use bitget:
n = hex2dec('AF75'); %for example, bit pattern is 1010 1111 0111 0101.
bitpattern = bitget(n, 1:8, 'uint16')
returns
bitpattern =
1 0 1 0 1 1 1 0
You can also use dec2bin but that involves string conversion so is going to be slower.
  댓글 수: 2
Raldi
Raldi 2015년 4월 13일
I get
Error using bitget
Too many input arguments.
I have to remove 'uint16' from the arguments. Even if I do so though I get
Inputs must be non-negative integers.
I have vR2011b by the way.
Guillaume
Guillaume 2015년 4월 13일
편집: Guillaume 2015년 4월 13일
I'm not sure when the type specifier was added to bitget, I think it's fairly recent.
On older versions of matlab, this should work:
n = hex2dec('AF75'); %for example, bit pattern is 1010 1111 0111 0101.
bitpattern = bitget(uint16(n), 1:8)
That is convert your number (of type double) to an unsigned integer type.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by