### Tags: alli, binary, converting, dec2bin, decimal, form, integer, matlab, neat, produce, programming, vector, zeroscorresponding

# Converting decimal integer to binary vector

On Programmer » Matlab

5,335 words with 8 Comments; publish: Wed, 07 May 2008 10:45:00 GMT; (20093.75, « »)

Hi all

I'm looking for a neat way to produce a vector v with ones and zeros

corresponding to the binary form of a given decimal integer n.

dec2bin by itself is not the answer since that produces a string.

An ugly solution that I know of is

n = 123;

v = dec2bin(n) - 48;

This works since 0 and 1 happens to be next to each other in the

ASCII table, numbered 48 and 49 respectively. Adding or subtracting

to a string converts it to a vector with the ASCII numbers of its

characters.

Now, is there a neat ASCII-independent way to do this?

Thanks in advance

/Fred

*http://matlab.todaysummary.com/q_matlab_10414.html*

All Comments

Leave a comment...

- 8 Comments
- Fred wrote:
>

> Hi all

> I'm looking for a neat way to produce a vector v with ones and

> zeros

> corresponding to the binary form of a given decimal integer n.

> dec2bin by itself is not the answer since that produces a string.

> An ugly solution that I know of is

> n = 123;

> v = dec2bin(n) - 48;

> This works since 0 and 1 happens to be next to each other in the

> ASCII table, numbered 48 and 49 respectively. Adding or subtracting

> to a string converts it to a vector with the ASCII numbers of its

> characters.

> Now, is there a neat ASCII-independent way to do this?

> Thanks in advance

> /Fred

Hi!

Try:

v=dec2bin(n)-'0';

HTH

/PB

#1; Wed, 07 May 2008 10:47:00 GMT

- Fred wrote:
- PB wrote:
>

> Fred wrote:

string.

the

> subtracting

its

> Hi!

> Try:

> v=dec2bin(n)-'0';

You could do worse that having a look at TMW's dec2bin function. It

is an m-file. I guess you could write your own without the char()

conversion at the end. The meat of the code looks like (d is your

dec value):

n=1;

[f,e]=log2(max(d));

rem(floor(d*pow2(1-max(n,e):0)),2)

#2; Wed, 07 May 2008 10:48:00 GMT

- PB wrote:
- PB wrote:
>

> Fred wrote:

string.

the

> subtracting

its

> Hi!

> Try:

> v=dec2bin(n)-'0';

Well, it works. It still depends on '0' and '1' being next to each

other in the character table though. I was thinking that there were a

more general preferred way, making use of other convention functions

or something. I happy with this, but anyone else trying to understand

such code would be puzzled.

/Fred

#3; Wed, 07 May 2008 10:49:00 GMT

- PB wrote:
- On Mon, 06 Mar 2006 11:03:17 -0500, Fred wrote:
> Well, it works. It still depends on '0' and '1' being next to each

> other in the character table though. I was thinking that there were a

> more general preferred way, making use of other convention functions

> or something. I happy with this, but anyone else trying to understand

> such code would be puzzled.

How about this?

v=dec2bin(n)=='1';

Dan

> /Fred

#4; Wed, 07 May 2008 10:50:00 GMT

- On Mon, 06 Mar 2006 11:03:17 -0500, Fred wrote:
- Fred:
< SNIP .. looking for a less ugly appraoch of

> n = 123;

> v = dec2bin(n) - 48;

n = 123 ;

v = bitget(n,ceil(log2(n):-1:1))

hth

Jos

#5; Wed, 07 May 2008 10:51:00 GMT

- Fred:
- Jos wrote:
> n = 123 ;

> v = bitget(n,ceil(log2(n):-1:1))

Interesting idea, but that particular line doesn't work.

You got a parenthesis wrong and the case n = 2^m, m integer

is not treated correctly. A working line would be

v = bitget(n,floor(log2(n))+1:-1:1)

n = 0 wouldn't work anyhow because of the logarithm.

Thank for the help though, I wouldn't have come up with this without

your input.

/Fred

#6; Wed, 07 May 2008 10:52:00 GMT

- Jos wrote:
- Fred wrote:
>

> Jos wrote:

>

> Interesting idea, but that particular line doesn't work.

> You got a parenthesis wrong and the case n = 2^m, m integer

> is not treated correctly. A working line would be

> v = bitget(n,floor(log2(n))+1:-1:1)

> n = 0 wouldn't work anyhow because of the logarithm.

> Thank for the help though, I wouldn't have come up with this

> without

> your input.

> /Fred

and this works for n = 0 as well:

v = bitget(n,max(ceil(log2(n+1)),1):-1:1) ;

Jos

#7; Wed, 07 May 2008 10:53:00 GMT

- Fred wrote:
- Jos wrote:
> and this works for n = 0 as well:

> v = bitget(n,max(ceil(log2(n+1)),1):-1:1) ;

Yes it does, well done!

The reason for asking about binary vectors in the first place, was

that I was looking for a neat way to produce all possible nxn

matrices with ones and zeros. My idea was to

enumerate them with a decimal number, convert it to a binary vector,

add extra zeros and finally reshape. Using bitget a realised that the

procedure could be simplified.

n = 3;

N = 2^(n^2); %The total number of possible nxn matrices

for k=0:N-1

A = bitget(k,1:n^2);

A = reshape(A,n,n);

disp(A);

end

No explicit adding of zeros and no logarithm here.

I am quite happy with this. Of couse I am doing more than just

printing the matrices. Now, is there a way to avoid reshape by for

example modifying the second argument in bitget to get an nxn matrix

directly instead of a 1xn^2 vector?

/Fred

#8; Wed, 07 May 2008 10:54:00 GMT

- Jos wrote: