bit vector

S

sipetar

HI.first of all i am sorry on my very bad english(spelling and grammar)
I have one problem.. need to write program that simulates Eratostens
riddle(in C) using bit vector, but I dont know how to implement bit
vector
and how to use it.

I've got something like this:

#define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
#define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
#define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))

but I dont understand this, so if you can help me i would appreciate
that..

THANK YOU...
 
M

Michael Mair

HI.first of all i am sorry on my very bad english(spelling and grammar)
I have one problem.. need to write program that simulates Eratostens
riddle(in C) using bit vector, but I dont know how to implement bit
vector
and how to use it.

I've got something like this:

#define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7))
#define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7)))
#define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7)))

but I dont understand this, so if you can help me i would appreciate
that..

As this is obviously a homework question, I just will recap some ideas:

We have an array
unsigned char v[MAXNUMBER];
We mark every number i from 2..MAXNUMBER-1 as potential prime number
by setting v to 1, then apply the sieve method to get rid of
non-prime numbers. In the end, we know all prime numbers in the above
range as only they will still have a 1 entry in v.

As the number of bits per byte (CHAR_BIT) is at least 8, we think
"Hey, if we store the information bitwise, we can save factor 8
and can get up to 8*MAXNUMBER-1 with the same array".
Now, we want to save the information for j*8+0..j*8+7 in v[j] and
retrieve it from there. This is where the above macros come in.
You could alternatively write them as
#define BIT_SET(v, n) (v[(n)/8] |= 1U << ((n)%8))
#define BIT_CLR(v, n) (v[(n)/8] &= ~(1U << ((n)%8)))
#define BIT_ISSET(v, n) (v[(n)/8] & (1U << ((n)%8)))
where BIT_SET sets for n=8*j+k the k'th bit of v[j]
(i.e. the overall n'th bit) to 1, BIT_CLR sets it to 1
and BIT_ISSET gives you 0 if the bit is not set and non-zero
if it is set.


Cheers
Michael
 
S

sipetar

yes it is my homework (I am student), and sorry if I bothering you
but i dont have much chioce....
 
C

Chris Croughton

yes it is my homework (I am student), and sorry if I bothering you
but i dont have much chioce....

Yes you do have a choice, you can ask your tutor. It's their job to
explain if you didn't understand in the class (you did go to the class,
right?). And the purpose of homework is to see whether the student has
understood, not to see whether they can get someone else to do it for
them. You've already been given an explanation here of what the macros
are for and how to go avout solving the problem...

Chris C
 
B

Ben Pfaff

Chris Croughton said:
Yes you do have a choice, you can ask your tutor.

It's a big assumption that a student has a tutor. That's (as I
understand it) part of the British system of education, but not
part of the US system.

In the US, I'd tell the student to ask the course instructor or a
teaching assistant for help.
 
R

Randy Howard

yes it is my homework (I am student), and sorry if I bothering you
but i dont have much chioce....

Here's a hint. Take each one of the macros, and write some simple
test code to take a known value (bit pattern if you like) and
see what they do. Print out the contents of them before and
after the BET_SET and BIT_CLR operations. Once you see it with
your own eyes it should make more sense. That's going to do
you a lot more good in the long run than somebody just telling
you what to do.
 
S

sipetar

I cant understand everything what my tutor say, because he explain
that on quite abstract and ambiguous way, and I am just beginer in
programming(I am 23 old, I started to programming before 2 years(I got
pc before 4)) so the easiest way is to ask on the news group...here
... and hope that somebody will answer may question....
by the way I am from Croatia..not US....and there is not many books on
internet writen on croatian language .. (you can see how my english
is bad)...

anyway thanks to everyone
 
R

Randy Howard

I cant understand everything what my tutor say,

At least you have fun. Not everyone does.
because he explain that on quite abstract and ambiguous way,

So ask him to provide a concrete example, on paper, showing every
step the next time you have a problem understanding him.

If he refuses, you have the wrong tutor.
 
R

Randy Howard

I cant understand everything what my tutor say,

At least you have fun. Not everyone does.
because he explain that on quite abstract and ambiguous way,

So ask him to provide a concrete example, on paper, showing every
step the next time you have a problem understanding him.

If he refuses, you have the wrong tutor.

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
 
M

Mark McIntyre

In the US, I'd tell the student to ask the course instructor or a
teaching assistant for help.

As far as anyone in the UK and probably Europe is concerned, the three are
synonyms.
 
C

Chris Croughton

It's a big assumption that a student has a tutor. That's (as I
understand it) part of the British system of education, but not
part of the US system.

In the US, I'd tell the student to ask the course instructor or a
teaching assistant for help.

Whether you call them a 'tutor', 'course instructor', 'Lehrer' or
whatever is not relevant, I'm referring to "the person who is teaching
you and who gave you the homework". That applies even to remote
schooling (learning by mail / email / TV / etc.).

Chris C
 
S

sipetar

Chris said:
Whether you call them a 'tutor', 'course instructor', 'Lehrer' or
whatever is not relevant, I'm referring to "the person who is teaching
you and who gave you the homework". That applies even to remote
schooling (learning by mail / email / TV / etc.).

Chris C
He give us short explaination, but almost nobody understand what he
says...
 
F

Flash Gordon

He give us short explaination, but almost nobody understand what he
says...

Then you should go on mass to him and tell him that his explanation is
not good enough.
 
B

Ben Pfaff

Chris Croughton said:
Whether you call them a 'tutor', 'course instructor', 'Lehrer' or
whatever is not relevant, I'm referring to "the person who is teaching
you and who gave you the homework". That applies even to remote
schooling (learning by mail / email / TV / etc.).

So a "tutor" who fits in that category? In the US it would be
someone you hired on the side to help you better understand the
course material. Thus the confusion.
 
C

Chris Croughton

He give us short explaination, but almost nobody understand what he
says...

In which case you (all of you who don't understand) need to ask him for
a full explanation. If he still doesn't explain properly complain to
the head of the department, his superior or to whoever is in charge of
teaching. He is presumably being employed to teach, if no one is
understanding him then something needs to be done.

Letting him get away with bad teaching does not solve the problem.

Chris C
 
S

sipetar

Chris said:
In which case you (all of you who don't understand) need to ask him for
a full explanation. If he still doesn't explain properly complain to
the head of the department, his superior or to whoever is in charge of
teaching. He is presumably being employed to teach, if no one is
understanding him then something needs to be done.

Letting him get away with bad teaching does not solve the problem.

Chris C

first :somethings he explain well,and other not so well
sec: he will leave very soon, so nobody really care
 
S

sipetar

Ben said:
So a "tutor" who fits in that category? In the US it would be
someone you hired on the side to help you better understand the
course material. Thus the confusion.

I am from Croatia - here is everthing wrong( upside down)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top