Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Manipulation of Binary Numbers

Subject: Manipulation of Binary Numbers

From: bennymacca

Date: 05 Feb, 2008 01:31:08

Message: 1 of 8

Hi

I am working with a file that contains a serial data stream (i.e
binary data)
i an then storing this data into arrays. i.e the number 3 would look
like [ 0 0 1 1] etc

what i want is a way of adding these numbers together. i.e [0 0 1 1] +
[0 0 0 1] = [0 1 0 0]

at present, my idea is to write a short script that converts to
decimal, then add the other number, then convert back to an array
using dec2binvec and binvec2dec. a short function would do this, but i
would like an easier way to do this.

Any ideas?
Cheers,
Ben

Subject: Re: Manipulation of Binary Numbers

From: Roger Stafford

Date: 05 Feb, 2008 03:09:02

Message: 2 of 8

bennymacca <ben.c.mcdonald@gmail.com> wrote in message
<c50dec66-3a80-4d42-922b-
ddcaf5eaf357@l1g2000hsa.googlegroups.com>...
> Hi
>
> I am working with a file that contains a serial data stream (i.e
> binary data)
> i an then storing this data into arrays. i.e the number 3 would look
> like [ 0 0 1 1] etc
>
> what i want is a way of adding these numbers together. i.e [0 0 1 1] +
> [0 0 0 1] = [0 1 0 0]
>
> at present, my idea is to write a short script that converts to
> decimal, then add the other number, then convert back to an array
> using dec2binvec and binvec2dec. a short function would do this, but i
> would like an easier way to do this.
>
> Any ideas?
> Cheers,
> Ben
------------
  I don't have dec2binvec and binvec2dec to experiment with, but I seriously
doubt that a for-loop or while-loop operating directly on a binary vector,
which is what you would have to replace them with, can compete with
operation speeds for numbers in double (that is, double precision binary
floating point) form. Addition, subtraction, and multiplication are all done
with great speed, since that is one of the things modern computers'
microprocessor chips are specifically designed to do - operate on numbers
contained in single binary registers.

  It should also be pointed out that these quantities that are loosely called
"decimal" are actually stored in binary form, not decimal, so, in my opinion,
calling them decimal is a misnomer. True decimal format is used only for the
purpose of displaying their values to users, not for internal operations. Thus,
the conversion processes needed to be performed by dec2binvec and
binvec2dec should be very simple and fast.

Roger Stafford

Subject: Re: Manipulation of Binary Numbers

From: Yumnam Kirani Singh

Date: 05 Feb, 2008 05:12:22

Message: 3 of 8

you can do in the following way. First convert the two vectors into equivalent decimal using bin2decbin and then add togather. Convert the result back to biary vector using dec2binvec. You may need to used fliplr or wrev for your purpose.

Subject: Re: Manipulation of Binary Numbers

From: bennymacca

Date: 05 Feb, 2008 10:43:07

Message: 4 of 8

On Feb 5, 1:09 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> bennymacca <ben.c.mcdon...@gmail.com> wrote in message
>
> <c50dec66-3a80-4d42-922b-
> ddcaf5eaf...@l1g2000hsa.googlegroups.com>...
>
> > Hi
>
> > I am working with a file that contains a serial data stream (i.e
> > binary data)
> > i an then storing this data into arrays. i.e the number 3 would look
> > like [ 0 0 1 1] etc
>
> > what i want is a way of adding these numbers together. i.e [0 0 1 1] +
> > [0 0 0 1] = [0 1 0 0]
>
> > at present, my idea is to write a short script that converts to
> > decimal, then add the other number, then convert back to an array
> > using dec2binvec and binvec2dec. a short function would do this, but i
> > would like an easier way to do this.
>
> > Any ideas?
> > Cheers,
> > Ben
>
> ------------
> I don't have dec2binvec and binvec2dec to experiment with, but I seriously
> doubt that a for-loop or while-loop operating directly on a binary vector,
> which is what you would have to replace them with, can compete with
> operation speeds for numbers in double (that is, double precision binary
> floating point) form. Addition, subtraction, and multiplication are all done
> with great speed, since that is one of the things modern computers'
> microprocessor chips are specifically designed to do - operate on numbers
> contained in single binary registers.
>
> It should also be pointed out that these quantities that are loosely called
> "decimal" are actually stored in binary form, not decimal, so, in my opinion,
> calling them decimal is a misnomer. True decimal format is used only for the
> purpose of displaying their values to users, not for internal operations. Thus,
> the conversion processes needed to be performed by dec2binvec and
> binvec2dec should be very simple and fast.
>
> Roger Stafford

Come on roger, thats not what i was asking. of course numbers are
stored in binary in a computer - this is a given
What i was asking was effectively implementation of arithmetic on a
binary DATA type. i thought this was quite obvious, but obviously not.
i was not after a speed increase, i was after the convenience of not
having to change from binary to integer, back to binary etc.

yumnam has it right below, this is the method that i have used.

cheers,
ben

Subject: Re: Manipulation of Binary Numbers

From: David

Date: 05 Feb, 2008 11:01:02

Message: 5 of 8

bennymacca <ben.c.mcdonald@gmail.com> wrote in message
<c50dec66-3a80-4d42-922b-
ddcaf5eaf357@l1g2000hsa.googlegroups.com>...
> Hi
>
> I am working with a file that contains a serial data
stream (i.e
> binary data)
> i an then storing this data into arrays. i.e the number
3 would look
> like [ 0 0 1 1] etc
>
> what i want is a way of adding these numbers together.
i.e [0 0 1 1] +
> [0 0 0 1] = [0 1 0 0]
>
> at present, my idea is to write a short script that
converts to
> decimal, then add the other number, then convert back to
an array
> using dec2binvec and binvec2dec. a short function would
do this, but i
> would like an easier way to do this.
>
> Any ideas?
> Cheers,
> Ben

not without a lot more information. you say you have a
stream of binary data... this doesn't mean anything. any
serial data stream contains binary data, bits... but bits
is bits, concatenting bits out of a stream may or may not
make anything meaningful. for instance the series of bits
[1 0 1 0 1 1 1 1] could be the decimal number 175
represented in 8 bits, or it could be -81 decimal
expressed in 2's complement in 8 bits. or it could be the
first 8 bits of a 64 bit double precision number, or one
color of a 24 bit pixel in a huge image. or it could be
one letter of a text document. or maybe its 4 bits of one
character and 4 bits of the next one, how do you know
where the pieces you want to work on start and stop??
unless you know exactly what that group of bits represent
arbitrarily converting them to a number and doing some
kind of operation on the resulting number may or may not
make any sense.

Subject: Re: Manipulation of Binary Numbers

From: bennymacca

Date: 05 Feb, 2008 23:53:40

Message: 6 of 8

David, you have missed my point

When i meant binary data stream, i meant the bits themselves. what i
do with them is not the issue, and is irrelevant here.

all i wanted to know was the best way to perform operations on single
bits at a time. i already know what the bits mean, but this does not
change the arithmetic performed on then.

if the binary stream was meant to be interpreted as integers or other,
then i would have converted to that, and there would not have been a
problem. but matlab seems to have little support for a "binary data
type" which is what i would like.

anyway, problem is solved now, thanks very much for your help

David wrote:
> bennymacca <ben.c.mcdonald@gmail.com> wrote in message
> <c50dec66-3a80-4d42-922b-
> ddcaf5eaf357@l1g2000hsa.googlegroups.com>...
> > Hi
> >
> > I am working with a file that contains a serial data
> stream (i.e
> > binary data)
> > i an then storing this data into arrays. i.e the number
> 3 would look
> > like [ 0 0 1 1] etc
> >
> > what i want is a way of adding these numbers together.
> i.e [0 0 1 1] +
> > [0 0 0 1] = [0 1 0 0]
> >
> > at present, my idea is to write a short script that
> converts to
> > decimal, then add the other number, then convert back to
> an array
> > using dec2binvec and binvec2dec. a short function would
> do this, but i
> > would like an easier way to do this.
> >
> > Any ideas?
> > Cheers,
> > Ben
>
> not without a lot more information. you say you have a
> stream of binary data... this doesn't mean anything. any
> serial data stream contains binary data, bits... but bits
> is bits, concatenting bits out of a stream may or may not
> make anything meaningful. for instance the series of bits
> [1 0 1 0 1 1 1 1] could be the decimal number 175
> represented in 8 bits, or it could be -81 decimal
> expressed in 2's complement in 8 bits. or it could be the
> first 8 bits of a 64 bit double precision number, or one
> color of a 24 bit pixel in a huge image. or it could be
> one letter of a text document. or maybe its 4 bits of one
> character and 4 bits of the next one, how do you know
> where the pieces you want to work on start and stop??
> unless you know exactly what that group of bits represent
> arbitrarily converting them to a number and doing some
> kind of operation on the resulting number may or may not
> make any sense.

Subject: Re: Manipulation of Binary Numbers

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 06 Feb, 2008 00:06:44

Message: 7 of 8

In article <74d47d21-3f70-43a0-ab8d-b58d163ffae4@l32g2000hse.googlegroups.com>,
bennymacca <ben.c.mcdonald@gmail.com> wrote:

>When i meant binary data stream, i meant the bits themselves. what i
>do with them is not the issue, and is irrelevant here.

>all i wanted to know was the best way to perform operations on single
>bits at a time. i already know what the bits mean, but this does not
>change the arithmetic performed on then.

Your posted question was about the best way to group several bits
in order to perform operations on the group, which is a different
question than performing operations on single bits.


>if the binary stream was meant to be interpreted as integers or other,
>then i would have converted to that, and there would not have been a
>problem. but matlab seems to have little support for a "binary data
>type" which is what i would like.

Matlab Fixed-Point Toolbox
http://www.mathworks.com/products/fixed/description3.html

  The toolbox lets you use four modes for performing fixed-point arithmetic:

    Fully automatic mode, enabling you to use up to 65,535 bits for the
    sum or product

    Fully specified mode, letting you indicate the word length and binary
    point location for the sum or product
--
'Roberson' is my family name; my given name is 'Walter'.

Subject: Re: Manipulation of Binary Numbers

From: bennymacca

Date: 06 Feb, 2008 00:29:49

Message: 8 of 8

thanks walter,
your right

what i wanted to distinguish though is that i was working on ones and
zeros, not on say an 8-bit integer grouped from the binary data
stream, in which case it would make more sense to convert the number
first before performing operations.

Regards,
ben

Walter Roberson wrote:
> In article <74d47d21-3f70-43a0-ab8d-b58d163ffae4@l32g2000hse.googlegroups.com>,
> bennymacca <ben.c.mcdonald@gmail.com> wrote:
>
> >When i meant binary data stream, i meant the bits themselves. what i
> >do with them is not the issue, and is irrelevant here.
>
> >all i wanted to know was the best way to perform operations on single
> >bits at a time. i already know what the bits mean, but this does not
> >change the arithmetic performed on then.
>
> Your posted question was about the best way to group several bits
> in order to perform operations on the group, which is a different
> question than performing operations on single bits.
>
>
> >if the binary stream was meant to be interpreted as integers or other,
> >then i would have converted to that, and there would not have been a
> >problem. but matlab seems to have little support for a "binary data
> >type" which is what i would like.
>
> Matlab Fixed-Point Toolbox
> http://www.mathworks.com/products/fixed/description3.html
>
> The toolbox lets you use four modes for performing fixed-point arithmetic:
>
> Fully automatic mode, enabling you to use up to 65,535 bits for the
> sum or product
>
> Fully specified mode, letting you indicate the word length and binary
> point location for the sum or product
> --
> 'Roberson' is my family name; my given name is 'Walter'.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics