Changing the bits randomly

조회 수: 2 (최근 30일)
kash
kash 2012년 11월 9일
I have a binary value
10000100000 which is 1056
i am changing last 5 bits
10000111111 which is 1087
now keeping last 5 bits constant i have to change other bits to get closer value of 1056
for ex i have changed one bit from 1 to 0
10000011111 which is 1055..like this i have to check,please tell how to process in a loop and check all bits ,manually i checked ,but i need in a for loop,or using GA
PLEASE HELP

답변 (3개)

Jan
Jan 2012년 11월 9일
x = 1087;
for bit = 6:16
orig = bitget(x, bit);
new = bitset(x, bit, 1-orig);
if new < x
x = new;
end
end
  댓글 수: 1
kash
kash 2012년 11월 9일
Jan can u please tell which variable is the output
because in new variable i get value as 32799
and in x i get 31 in which two variables does not give the result which is close to 1056,plese provide assistance

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


C.J. Harris
C.J. Harris 2012년 11월 9일
Try this, it is a somewhat brute force approach to solving your problem. The value 'nMatch' should give you the desired answer:
nInput = 1056;
A = dec2bin(nInput);
A(end-4:end) = '1';
nRest = 2^length(A(1:end-5)) - 1;
nCombs = dec2bin(0:nRest);
maxErr = Inf;
for nCount = 1:nRest
testVal = bin2dec([nCombs(nCount,:) A(end-4:end)]);
if abs(nInput - testVal) < maxErr
maxErr = abs(nInput - testVal);
nMatch = testVal;
end
end

C.J. Harris
C.J. Harris 2012년 11월 9일
or less brute force without a for loop:
nInput = 1056;
nShift = bin2dec('11111');
nOffset = nInput - nShift;
nNear = round(nOffset/(nShift + 1));
nMatch = nNear*(nShift + 1) + nShift;
  댓글 수: 1
kash
kash 2012년 11월 9일
ok thanks harris ,but i want to perform for wav signals
I have to embed 2nd wave signal in 1st wave signal
the 2nd wave bit length is less than 1st,
please can u help
assuming the 1st wave signal length for ex
100010101010101000000
second is
11111001
i need to embed 2nd in 1st
100010101010111111001 ans then process it

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

카테고리

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