From: Ujjwal Verma on
I wrote a program which explain in detail the problem I am facing. I will be thankful if you could kindly look into it
clear all;
clc;

ng=43.65; %we will be working on this number

% seperate the decimal(dec1) and integer(int1) part
int1 = floor(ng);
dec1 = 100*(ng-int1);

% convert to binary
% here it works fine!
out = dec2bin(dec1,7);
binary=logical(str2num([fliplr(out);blanks(length(out))]')');


%change the bit corresponding to this bit index
bit_index = 1;
binary(bit_index)=1;


%convert back to decimal
h = deblank(num2str(fliplr(binary)'))';

% Convert the binary string to a decimal number.
dec1 = bin2dec(h);

ng = int1+dec1/100;


% seperate the decimal and integer part
int2= floor(ng);
dec2 = 100*(ng-int2);

% convert to binary
% the problem is here as it gives dec2binvec(dec2,7)=0000001
out = dec2bin(dec2,7);
binary2=logical(str2num([fliplr(out);blanks(length(out))]')');

% to verify whether LSB is correct or not
if(binary2(bit_index)==1)
disp('It works!!');
end



"us " <us(a)neurol.unizh.ch> wrote in message <htlqcm$c0e$1(a)fred.mathworks.com>...
> "Ujjwal Verma"
> > yes it comes from dec2bin (switch case). I have rather spotted the place of error, its in the last line of dec2bin s=char(rem(floor(d*pow2(1-max(n,e):0)),2)+'0');
> > (cf my last post)
>
> sorry, didn't see your post before i replied...
> anyhow, this is REALLY strange, not to say: inexplicable...
> DEC2BIN has been around for ages without any complaint... and as far as i know its subroutines, eg, LOG2, POW2, have not been reported as being buggy (your r2008 or before)...
> here, DEC2BIN works as expected...
> note, though
> % Copyright 1984-2009 The MathWorks, Inc.
> % $Revision: 1.13.4.9 $ $Date: 2009/08/14 04:01:50 $
> and the relevant lines are
>
> d = double(d);
> if nargin<2
> n=1; % Need at least one digit even for 0.
> else
> if ~(isnumeric(n) || ischar(n)) || ~isscalar(n) || n<0
> error('MATLAB:dec2bin:InvalidBitArg','N must be a positive scalar numeric.');
> end
> n = double(n);
> n = round(n); % Make sure n is an integer.
> end;
> %
> % Actual algorithm
> %
> [f,e]=log2(max(d)); % How many digits do we need to represent the numbers?
> s=char(rem(floor(d*pow2(1-max(n,e):0)),2)+'0');
>
> unfortunately, we don't run earlier versions anymore...
> therefore, i cannot check your data...
>
> us
From: Ujjwal Verma on
Sorry for duplicate posts....but this program is more clear....as it uses dec2binvec....earlier one uses only de2bin

clear all;
clc;

ng=43.65; %we will be working on this number

% seperate the decimal(dec1) and integer(int1) part
int1 = floor(ng);
dec1 = 100*(ng-int1);

% convert to binary
% here it works fine!

binary=dec2binvec(dec1,7);


%change the bit corresponding to this bit index
bit_index = 1;
binary(bit_index)=1;


%convert back to decimal

dec1 = binvec2dec(binary);

ng = int1+dec1/100;


% seperate the decimal and integer part
int2= floor(ng);
dec2 = 100*(ng-int2);

% convert to binary
% the problem is here as it gives dec2binvec(dec2,7)=0000001

binary2=dec2binvec(dec2,7);

% to verify whether LSB is correct or not
if(binary2(bit_index)==1)
disp('It works!!');
end


"us " <us(a)neurol.unizh.ch> wrote in message <htlqcm$c0e$1(a)fred.mathworks.com>...
> "Ujjwal Verma"
> > yes it comes from dec2bin (switch case). I have rather spotted the place of error, its in the last line of dec2bin s=char(rem(floor(d*pow2(1-max(n,e):0)),2)+'0');
> > (cf my last post)
>
> sorry, didn't see your post before i replied...
> anyhow, this is REALLY strange, not to say: inexplicable...
> DEC2BIN has been around for ages without any complaint... and as far as i know its subroutines, eg, LOG2, POW2, have not been reported as being buggy (your r2008 or before)...
> here, DEC2BIN works as expected...
> note, though
> % Copyright 1984-2009 The MathWorks, Inc.
> % $Revision: 1.13.4.9 $ $Date: 2009/08/14 04:01:50 $
> and the relevant lines are
>
> d = double(d);
> if nargin<2
> n=1; % Need at least one digit even for 0.
> else
> if ~(isnumeric(n) || ischar(n)) || ~isscalar(n) || n<0
> error('MATLAB:dec2bin:InvalidBitArg','N must be a positive scalar numeric.');
> end
> n = double(n);
> n = round(n); % Make sure n is an integer.
> end;
> %
> % Actual algorithm
> %
> [f,e]=log2(max(d)); % How many digits do we need to represent the numbers?
> s=char(rem(floor(d*pow2(1-max(n,e):0)),2)+'0');
>
> unfortunately, we don't run earlier versions anymore...
> therefore, i cannot check your data...
>
> us