From: jim holtman <jholtman_at_gmail.com>
Date: Sat 13 May 2006 - 07:07:33 EST

I just googled "counting bits in a word"

On 5/12/06, Nameeta Lobo <nlobo@bsd.uchicago.edu> wrote:
>
> Hi again
>
> Sorry I should have been more specific the first time. I am not actually
> trying
> to generate this matrix but what am trying to do is to write a loop which
> takes
> an input of 0000, does a bitwise addition, then checks to see if the new
> number
> generated has two 1s(I just summed the values to get 2) and if it does
> outputs
> that to a matrix. It goes on doing it till it reaches 1111.
>
> So my final output for the above would read
> 0011
> 0101
> 0110
> 1001
> 1010
> 1100
>
> For 2^6, I was looking for three 1s and so on.
>
> I wrote this in a for loop etc. Created the original matrix with
> expand.grid
> command that you guys helped me out with before.
>
> My only concern was that for e.g. 2^25 or higher, not much space can be
> allocated to the creation of the original matrix and then also because of
> the
> loops there is the problem of computer time. So was just wondering if it
> was
> easier to do it via bitwise addition.
>
> thanks a lot for your help everyone.
>
> any more suggestion!!!!
>
> nameeta
>
>
>
>
>
> Quoting "Marc Schwartz (via MN)" <mschwartz@mn.rr.com>:
>
> > On Fri, 2006-05-12 at 14:05 -0500, Marc Schwartz (via MN) wrote:
> > > On Fri, 2006-05-12 at 11:41 -0500, Nameeta Lobo wrote:
> > > >
> > > > Hello all again,
> > > >
> > > > I want to do bitwise addition in R. I am trying to generate a matrix
> > > > 0000
> > > > 0001
> > > > 0010
> > > > ....
> > > > ....
> > > > 1111
> > > >
> > > > I know the other ways of generating this matrix but I need to look
> at
> > bitwise
> > > >
> > > > Any suggestions???
> > > >
> > > > thanks a lot
> > > >
> > > > Nameeta
> > >
> > > Nameeta,
> > >
> > > I may be misunderstanding what you are trying to do, so here are two
> > > approaches that might be helpful:
> > >
> > > 1. Presuming that each of the above rows is a binary representation of
> a
> > > number x >= 0 (so we don't have to worry about two's complements) and
> > > that you want to add the rows to get a total, you can do:
> > >
> > > > mat
> > > [,1] [,2] [,3] [,4]
> > > [1,] 0 0 0 0
> > > [2,] 0 0 0 1
> > > [3,] 0 0 1 0
> > > [4,] 1 1 1 1
> > >
> > > # This will convert each row to it's base 10 value
> > > > apply(mat, 1, function(x) sum(x * (2 ^ ((length(x) - 1):0))))
> > > [1] 0 1 2 15
> > >
> > > # So just sum them
> > > > sum(apply(mat, 1, function(x) sum(x * (2 ^ ((length(x) - 1):0)))))
> > > [1] 18
> > >
> > >
> > > 2. If you want to actually generate the above matrix as a sequence of
> > > binary values from a sequence of base 10 integer values, you can use
> the
> > > digitsBase() function in Martin's sfsmisc package on CRAN:
> > >
> > > install.packages("sfsmisc")
> > > library(sfsmisc)
> > >
> > > > t(digitsBase(1:15))
> > > Class 'basedInt'(base = 2) [1:4]
> > > [,1] [,2] [,3] [,4]
> > > [1,] 0 0 0 1
> > > [2,] 0 0 1 0
> > > [3,] 0 0 1 1
> > > [4,] 0 1 0 0
> > > [5,] 0 1 0 1
> > > [6,] 0 1 1 0
> > > [7,] 0 1 1 1
> > > [8,] 1 0 0 0
> > > [9,] 1 0 0 1
> > > [10,] 1 0 1 0
> > > [11,] 1 0 1 1
> > > [12,] 1 1 0 0
> > > [13,] 1 1 0 1
> > > [14,] 1 1 1 0
> > > [15,] 1 1 1 1
> >
> >
> > One quick notation here. I just noted that Martin has an as.integer()
> > method for the basedInt class above. This allows for converting the
> > numbers back to base 10 representation. It presumes that the number to
> > be converted is the column, not the row. Thus the transpose of the
> > matrix above results in:
> >
> > > as.integer(t(digitsBase(1:15)))
> > 1 2 3 4
> > 255 3855 13107 21845
> >
> > If you of course leave the matrix as is (as Martin clearly intended),
> > you get:
> >
> > > digitsBase(1:15)
> > Class 'basedInt'(base = 2) [1:15]
> > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> > [1,] 0 0 0 0 0 0 0 1 1 1 1 1
> > [2,] 0 0 0 1 1 1 1 0 0 0 0 1
> > [3,] 0 1 1 0 0 1 1 0 0 1 1 0
> > [4,] 1 0 1 0 1 0 1 0 1 0 1 0
> > [,13] [,14] [,15]
> > [1,] 1 1 1
> > [2,] 1 1 1
> > [3,] 0 1 1
> > [4,] 1 0 1
> >
> > and therefore:
> >
> > > as.integer(digitsBase(1:15))
> > 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
> > 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
> >
> >
> > This would make basic arithmetic operations easier. Generating 'mat'
> > above for example:
> >
> > > mat <- digitsBase(c(0:2, 15))
> >
> > # Again, note that each number is a column
> > # not the row
> > > mat
> > Class 'basedInt'(base = 2) [1:4]
> > [,1] [,2] [,3] [,4]
> > [1,] 0 0 0 1
> > [2,] 0 0 0 1
> > [3,] 0 0 1 1
> > [4,] 0 1 0 1
> >
> > > as.integer(mat)
> > 1 2 3 4
> > 0 1 2 15
> >
> >
> > Finally:
> >
> > > sum(as.integer(mat))
> > [1] 18
> >
> > and back to binary:
> >
> > > digitsBase(sum(as.integer(mat)))
> > Class 'basedInt'(base = 2) [1:1]
> > [,1]
> > [1,] 1
> > [2,] 0
> > [3,] 0
> > [4,] 1
> > [5,] 0
> >
> > or even as a character vector:
> >
> > > paste(digitsBase(sum(as.integer(mat))), collapse = "")
> > [1] "10010"
> >
> >
> > HTH,
> >
> > Marc Schwartz
> >
> >
> >
> >
> >
>
>
>
>
>
> -------------------------------------------------
> This email is intended only for the use of the individual or...{{dropped}}
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
>

```--
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)

What is the problem you are trying to solve?

[[alternative HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help