COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### binary no

• Email
• Follow

```How can we count no of ones in a binary no.
plz don't tell by linear search. if any effecient logic u have in
mind...plz tell me.

```
 0
Reply nehilparashar (24) 7/19/2007 11:54:33 AM

See related articles to this posting

```On Thursday 19 Jul 2007 5:24 pm, Nehil <nehilparashar@gmail.com>
wrote in message

> How can we count no of ones in a binary no.
> plz don't tell by linear search. if any effecient logic u have in
> mind...plz tell me.
>

Since your question is a general algorithmic one,
news:comp.programming will be a better forum than this one.

```
 0
Reply santosh.k83 (3969) 7/19/2007 12:10:51 PM

```Nehil wrote:

> How can we count no of ones in a binary no.

Binary "no" is 0, which has zero 1's in it.

> plz don't tell by linear search. if any effecient logic u have in
> mind...plz tell me.

One trick is to use `whatever = whatever & (whatever - 1)`. (Of /course/
in a loop.)

Of course, googling "count bits" couldn't possibly get any answers, because
if it did, you wouldn't have asked the question.

--
Chris "cranky today" Dollin

Hewlett-Packard Limited                                          registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN          690597 England

```
 0
Reply chris.dollin (1683) 7/19/2007 1:20:03 PM

```Nehil wrote:

> How can we count no of ones in a binary no.
> plz don't tell by linear search. if any effecient logic u have in
> mind...plz tell me.

Fast ways are lookup table and hardware adder logic.  In terms of the
lookup table, the number can be split into n-bit chunks for lookup into
a table with 2^n entries.

--
```
 0

3 Replies
34 Views

Similar Articles

12/7/2013 3:58:28 PM
page loaded in 72794 ms. (0)

Similar Artilces:

help binary?
I've got a ".sef" file in binary format that I would like to read...problem is I don't know really know how to use fread to get what I want. Can anyone help?....Please this is urgent. basically the file formats is supposed to be like this: string ....... numbers (32 bits) ....... string ...... number (float 32) ....... string ....... number (32 bits float). how do I deal with something like this? Regards, "Francki " <jonesfranckandi@yahoo.fr> wrote in message <i92ig4\$q9h\$1@fred.mathworks.com>... > I've got a ".sef" file in binary format that I would like to read...problem is I don't know really know how to use fread to get what I want. Can anyone help?....Please this is urgent. > > basically the file formats is supposed to be like this: > string > ...... > numbers (32 bits) > ...... > string > ..... > number (float 32) > ...... > string > ...... > number (32 bits float). > > how do I deal with something like this? > > Regards, You are in luck! MatLab has a function just for you. Look up the help section for fread "

binary const
Hello all, is there a way in order to write binary numbers as hexa or octal in c ? Xavier serrand wrote: > Hello all, > > is there a way in order to write binary numbers as hexa or octal in c ? I assume you are asking whether C supports a binary constant notation like it does for octal and hexadecimal numbers, the answer is no. It is not too difficult to create macros that allow you to do this though. Robert Gamble Robert Gamble wrote: > serrand wrote: > >>Hello all, >> >>is there a way in order to write binary numbers as hexa or octal in c ? > > > I assume you are asking whether C supports a binary constant notation > like it does for octal and hexadecimal numbers, the answer is no. It > is not too difficult to create macros that allow you to do this though. > > Robert Gamble > Can you give me an example of a macro to do this? Thanks. -- Joe Wright "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Joe Wright wrote: > Robert Gamble wrote: > > serrand wrote: > > > >>Hello all, > >> > >>

binary to octagon
is there any command to convert directly binary no. matrix to octagon no. matrix,if not then how can i convert binary to octagon On 30/11/10 9:18 PM, nidhi wrote: > is there any command to convert directly binary no. matrix to octagon > no. matrix,if not then how can i convert binary to octagon I don't think you mean "octagon", I think you mean "octal". Presuming that the matrix B is a two dimension array consisting of the number values 0 and 1, and that the rows are to be converted into octal digits, and that the first column of the array is the "most significant digit": B = [zeros(size(B,1),mod(-size(B,2),3)),B] %pad with leading 0s oct = reshape(sum(bsxfun(@times,reshape(B.',3,[]),[4 2 1].')),[],size(B,1)).'; For example, B = 1 1 0 1 1 1 0 0 0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 0 0 0 1 0 1 0 0 0 1 0 0 0 would become oct = 3 3 2 0 2 3 3 3 1 7 3 0

Decimal to binary
Is there a function available to convert negative decimal numbers to binary. I need to convert positive and negative numbers that are in a matrix and output the converted binary to a file. In article <fakos4\$2jm\$1@fred.mathworks.com>, Anthony <anthony.campos@baesystems.com> wrote: >Is there a function available to convert negative decimal >numbers to binary. dec2bin(typecast(int32(Numbers),'uint32')) If the numbers are smaller, use int16 or int8 as appropriate. Warning: if the numbers are orginally double and they exceed 2^31-1 then the int32(Numbers) step will use 2^31-1 in place of any such number. Matlab's double can exactly store (no round-off) integers up to 2^52 (or is it 2^53?) but those are too large to hold in an int32 . -- All is vanity. -- Ecclesiastes

binary image convex hull
Hi, Here I post a convex hull code which can be used for constructing a convex hull of boundary points in the binary raster image. I borrowed the basic idea from the chain hull algorithm or Melkman algorithm where pre-ordering of points is necessary. Raster scanning makes such an ordering natually such that by just single scanning of image we will have an convex hull of boundary point of binary object in the image. This may be useful to image processing based recognition area. Enjoys! #define bg 0x00 /* forground color */ #define fg 0xFF /* background color*/ /* > 0 ; counter clockwise order */ /* =0 ; C is on the line AB; */ /* <0 ; clockwise order; */ #define CCW(A,B,C) ((B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x)) /* make a convex hull of boundary of forground object in the binary image */ /* in some case L[0]=R[0], or L[ll]=R[rr] if first line or last line of object is composed of ** a single point */ int RasterHull (BYTE *image, int w, int h, POINT H[]) { POINT *L = new POINT [h]; //stack of left-side hull; POINT *R= new POINT [h]; //stack of right side hull; int rr=-1, ll=-1; POINT Q; for(int

Binary image transformation function
Hi I'm looking for some good image transformation function(s) form my program. I'm trying to use NN to classify hand gestures. That's why I need to transform my binary hand gesture image into vector (for training and testing). I looking for function(s) that is(are): * able to give me quite small vector (less then 320 fields) * able to return similar vector (for the same gestures) values when changing angle of hand gesture in front of camera * able to return similar vector (for the same gestures) when changing distance to camera I know that I might need more then one function. That's OK. Right now I just looking for some clues. Right now I'm counting black pixels in each image column, so I get vector of 320 values. It works quite OK, but it's sensitive to distance and angle of showing gesture. Sample images: http://dienet.fm.interia.pl/hand.png http://dienet.fm.interia.pl/hand2.png Thank you for any help. -- pozdr0 dienet

Combinations in a binary matrix
as you process it. James Tursa Thanks James, but unfortunately this returns a column vector of binary combinations. Any idea how to separate the each element so as to create, for your example, a 8x3 matrix?

plotting binary sequences
Hello, how can I plot a signal as an rectangle instead of an triangle? e.g.: x = [-ones(5,1);ones(1,1);-ones(5,1);ones(1,1);-ones(5,1)]; in Simulink x is a _ _ ___| |____| |___ in Matlab it's ___/\____/\___ Is there a general solution to solve this problem? Thanks, Matthias. "Matthias Stock" <Matthias.Stock@REMunibw-muenchen.de> wrote in message news:403cc7f3@news.unibw-muenchen.de... > Hello, > > how can I plot a signal as an rectangle instead of an triangle? > e.g.: x = [-ones(5,1);ones(1,1);-ones(5,1);ones(1,1);-ones(5,1)]; > >

Binary to BCD in VHDL
Hello all. I'm very new to VHDL and stuck with a simple task. This code should convert binary number to BCD number using shift and add= 3 = algoritam http://www.engr.udayton.edu/faculty/jloomis/ece314/notes/devices/binary_= to_BCD/bin_to_BCD.html After this code executes I always get "0000" in digit and unit :-( You don't have to analyze the whole code, just tell me what is generally= = wrong. Thank you people! variable temp: bit_vector(7 downto 0) :=3D "00011000"; --24 (10) variable unit: bit_vector(3 downto 0) :=3D "0000"; variable digit: bit_vector(3 downto 0):=3D "0000"; begin for i in 0 to 7 loop digit :=3D digit sll 1; digit(0) :=3D unit(3); unit :=3D unit sll 1; unit(0) :=3D temp(7); temp :=3D temp sll 1; --This is the part where I add 3, is there any other way? It must work= = on FPGA case digit is when "0101" =3D> digit :=3D "1000"; when "0110" =3D> digit :=3D "1001"; when "0111" =3D> digit :=3D "1010"; when "1000" =3D> digit :=3D "1011"; when "1001" =3D>

binary image #3
hi, if I want to convert grayscal image to binary image .what should I used? when I take threshold and put the pixel above it 1 and less than it 0 all image will be black. thanks in advance Hi, try this : X=rand(50); binX=X>0.5; figure('Name','IMAGE FUNCTION') colormap(gray); image(binX); figure('Name','IMAGESC FUNCTION') colormap(gray); imagesc(binX); J�r�me On Sep 8, 1:04 am, "huda nawaf" <halmam...@yahoo.com> wrote: > hi, > if I want to convert grayscal image to binary image .what > should I used? > > when I...;1189258060.288067.5270@o80g2000hse.googlegroups.com>... > On Sep 8, 1:04 am, "huda nawaf" <halmam...@yahoo.com> wrote: > > hi, > > if I want to convert grayscal image to binary image .what > > should I used? > > > > when I take threshold and put the pixel above it 1 and > > less than it 0 all image will be black. > > > > thanks in advance > > You probably have your threshold too high. If it's all black, then the > image is all zeros. Try lowering your threshold. For a greyscale image > with 256 levels of grey, you're first attempt should