Changing the bits randomly

7 visualizzazioni (ultimi 30 giorni)
kash
kash il 9 Nov 2012
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

Risposte (3)

Jan
Jan il 9 Nov 2012
x = 1087;
for bit = 6:16
orig = bitget(x, bit);
new = bitset(x, bit, 1-orig);
if new < x
x = new;
end
end
  1 Commento
kash
kash il 9 Nov 2012
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

Accedi per commentare.


C.J. Harris
C.J. Harris il 9 Nov 2012
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 il 9 Nov 2012
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 Commento
kash
kash il 9 Nov 2012
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

Accedi per commentare.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by