Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Turning bits into integers

48 views
Skip to first unread message

rgla...@sfu.ca

unread,
Feb 15, 2019, 5:43:51 PM2/15/19
to
I have the following struct:


struct NonlocalBonds {
std::vector<unsigned int> k;
unsigned int l;

unsigned int bond = 0;
bond ^= 1<<k;

unsigned int operator()(unsigned int i, unsigned int j) const {
assert(j > i);

if ((j-i) % 4 != 0 || (j-i) / 4 != l)
return k.end();

if ((i-2) % 4 != 0 || std::find(k.begin(), k.end(), (i-2) / 4) == k.end())
return k.end();

return ((1-2 / 4);
}
};


Where k and l are contained within a json output file, and are shown below:

"nonlocal_bonds": {
"k": [0, 4, 9, 11, 16, 18, 23, 27, 29, 32, 35, 38, 41, 45, 47, 50, 52, 56],
"l": 5
},


I am using i and j, which are unsigned ints whose values are specified in another function, to calculate l and k from the expressions (j-i) / 4 and (i-2) / 4). If the calculated l and k are matched with the l and k from my output file, I want to return the k that matched with the value of k in the output file. Then, I want to do bond ^= 1<<k on the returned k.

I know there are lots of errors in here, and I need help.

I think I am supposed to be returning std::find(k.begin(), k.end(), (i-2) / 4)) - k.end() or something, and the type of operator() is probably wrong, and I don't know how to tell "bond" that the k in this equation is the returned value.

Jorgen Grahn

unread,
Feb 15, 2019, 6:51:15 PM2/15/19
to
On Fri, 2019-02-15, rgla...@sfu.ca wrote:
> I have the following struct:

I note that there's nothing in the text about "turning bits into
integers". Was the subject line an error, or a clue to what you
really wanted to ask about?

> struct NonlocalBonds {
> std::vector<unsigned int> k;
> unsigned int l;
>
> unsigned int bond = 0;
> bond ^= 1<<k;

I have no idea what this is supposed to mean. It's better to show
code that at least compiles.

> unsigned int operator()(unsigned int i, unsigned int j) const {
> assert(j > i);
>
> if ((j-i) % 4 != 0 || (j-i) / 4 != l)
> return k.end();
>
> if ((i-2) % 4 != 0 || std::find(k.begin(), k.end(), (i-2) / 4) == k.end())
> return k.end();
>
> return ((1-2 / 4);
> }
> };

This cannot compile, either. k.end() is an iterator and ((1-2 / 4) is
a syntax error.

You seem to have mixed up 1 and l, too. Never use l (or O) as
identifiers.

> Where k and l are contained within a json output file, and are shown below:
>
> "nonlocal_bonds": {
> "k": [0, 4, 9, 11, 16, 18, 23, 27, 29, 32, 35, 38, 41, 45, 47, 50, 52, 56],
> "l": 5
> },

That they come from a file in a certain format is irrelevant. Instead
show that you construct a NonlocalBonds with those values. Like, a
main() with a test case.

> I am using i and j, which are unsigned ints

Like the code says.

> whose values are specified in another function,

Like the code says (they are parameters).

> to calculate l and k from the expressions (j-i) / 4 and (i-2) /
> 4). If the calculated l and k are matched with the l and k from my
> output file, I want to return the k that matched with the value of k
> in the output file. Then, I want to do bond ^= 1<<k on the returned
> k.
>
> I know there are lots of errors in here, and I need help.

My main issue is you're writing a lot about the low-level operations
you intend to execute, but nothing about what you're trying to
accomplish. Unless the subject line is a clue, or unless Nonlocal
Bonds are well-known in some problem domain I'm ignorant
of. (Economics? Chemistry? Topology?)

I think you'd have more luck if you explained your problem from the
beginning.

> I think I am supposed to be returning std::find(k.begin(), k.end(),
> (i-2) / 4)) - k.end() or something,

Why? Note that k.end() is always "larger or equal to" find(...).

> and the type of operator() is
> probably wrong, and I don't know how to tell "bond" that the k in
> this equation is the returned value.

I don't know who "bond" is, and I see no equation.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Jux und Tollerei

unread,
Feb 16, 2019, 4:17:19 AM2/16/19
to
>> struct NonlocalBonds {
>> std::vector<unsigned int> k;
>> unsigned int l;
>>
>> unsigned int bond = 0;
>> bond ^= 1<<k;

> I have no idea what this is supposed to mean. It's better to show
> code that at least compiles.

Great code! It compiles here.

Alf P. Steinbach

unread,
Feb 16, 2019, 9:22:48 AM2/16/19
to
On 15.02.2019 23:43, rgla...@sfu.ca wrote:
> struct NonlocalBonds {
> std::vector<unsigned int> k;
> unsigned int l;
>
> unsigned int bond = 0;
> bond ^= 1<<k;
>
> unsigned int operator()(unsigned int i, unsigned int j) const {
> assert(j > i);
>
> if ((j-i) % 4 != 0 || (j-i) / 4 != l)
> return k.end();
>
> if ((i-2) % 4 != 0 || std::find(k.begin(), k.end(), (i-2) / 4) == k.end())
> return k.end();
>
> return ((1-2 / 4);
> }
> };

Invalid code & doesn't look meaningful to me.

Sorry,

- Alf

rgla...@sfu.ca

unread,
Feb 19, 2019, 1:06:25 PM2/19/19
to
Sorry for the late reply and thanks to those who responded. I agree that what I had programmed made no sense. I'm a beginner.

struct NonlocalBonds {
std::vector<unsigned int> k;
unsigned int l;

std::vector<unsigned int>::const_iterator operator()(unsigned int i, unsigned int j) const {
assert(j > i);

if ((j-i) % 4 != 0 || (j-i) / 4 != l || (i-2) % 4 != 0)
return k.end();

return std::find(k.begin(), k.end(), (i-2) / 4) - k.begin();
}
};

Here is the revised code. Basically, what I want is to output the index of a value inside the vector k, if it exists. The presence of this value is determined by the if statement. I'm not sure if the type of const_iterator is correct, but I think the rest is correct.

rgla...@sfu.ca

unread,
Feb 19, 2019, 1:13:51 PM2/19/19
to
I don't know if this is the best method to accomplish what I want to do.

Jorgen Grahn

unread,
Feb 19, 2019, 3:23:00 PM2/19/19
to
I still don't understand your goal, but with the above I can at least
make partial suggestions. Does this capture what you mean?

std::vector<unsigned>::const_iterator
operator() (unsigned i, unsigned j) const {
unsigned val = f(i, j);
return std::find(k.begin(), k.end(), val);
}

I broke out the calculation of the value to a separate helper function
f() so it doesn't distract.

You seem to be a bit undecided about if you want to return an iterator
into k, or an index in k. Sometimes one works best, sometimes the
other -- it depends on what the calling code wants. If you return an
index, you have to decide what to return if you don't find the value.

james...@alumni.caltech.edu

unread,
Feb 19, 2019, 3:27:30 PM2/19/19
to
On Tuesday, February 19, 2019 at 1:06:25 PM UTC-5, rgla...@sfu.ca wrote:
> Sorry for the late reply and thanks to those who responded. I agree that what I had programmed made no sense. I'm a beginner.
>
> struct NonlocalBonds {
> std::vector<unsigned int> k;
> unsigned int l;
>
> std::vector<unsigned int>::const_iterator operator()(unsigned int i, unsigned int j) const {
> assert(j > i);
>
> if ((j-i) % 4 != 0 || (j-i) / 4 != l || (i-2) % 4 != 0)

(j-1)%4 != 0 || (j-1)/4 != 1 is equivalent to
!((j-1)%4 == 0 && (j-1)/4 == 1) (de Morgan's laws).

The meaning of the % operator is defined by the following requirement,
if and only if a/b is representable in the result type (7.6.5p4):

(a/b)*b + (a%b) == a

Therefore, (j-1)%4 == 0 && (j-1)/4 == 1 implies that

1*4 + 0 = j-i

Therefore you can simplify that condition to

if(j != i + 4 || (i-2)%4 == 0)

Does that re-write correctly express the condition you intended to
express? If not, then (assuming I carried out the above simplifications
correctly), then neither does your original.
123456789012345678901234567890123456789012345678901234567890123456789012

Note also that for positive i, (i-2)%4==0 is equivalent to i%4 == 2 (for
negative values of i, it's equivalent to i%4 == -2). If you know that i
is always positive, that could also be used to simplify your code.

> return k.end();
>
> return std::find(k.begin(), k.end(), (i-2) / 4) - k.begin();

std::find() returns an iterator of the same type as passed into it.
Subtracting k.begin() from that iterator gives a value of the type
std::vector<unsigned int>::difference_type, which must be a signed
integer type.

> }
> };
>
> Here is the revised code. Basically, what I want is to output the index of a value inside the vector k, if it exists. The presence of this value is determined by the if statement. I'm not sure if the type of const_iterator is correct, but I think the rest is correct.

Your function should be returning
std::vector<unsigned int>::difference_type. Such a type will not, in
general, be implicitly convertible to an iterator type such as the one
you actually specified.
0 new messages