473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pointer, union, something...

typedef unsigned long long uint64 // just so we dont get confused ;)

ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all times
contain a value equal to the ORing of all the n variables. eg.

@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code

i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i think).
Oh, there is one unsignificant thing. It is know that that the
variable in y[n] will allways equal 0 if ANDed.

so, do i have to write a function which i have to call constantly, or
is there a smarter way?

NB. the y[n] variable must of course not be altered in anyway by this
operation.

Sep 18 '07 #1
8 1596
za******@gmail.com wrote:
typedef unsigned long long uint64 // just so we dont get confused ;)

ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all times
contain a value equal to the ORing of all the n variables. eg.

@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code

i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i think).
Oh, there is one unsignificant thing. It is know that that the
variable in y[n] will allways equal 0 if ANDed.

so, do i have to write a function which i have to call constantly, or
is there a smarter way?

NB. the y[n] variable must of course not be altered in anyway by this
operation.
You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.

The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 18 '07 #2
On 18 Sep., 22:57, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
zacar...@gmail.com wrote:
typedef unsigned long long uint64 // just so we dont get confused ;)
ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all times
contain a value equal to the ORing of all the n variables. eg.
@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code
i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i think).
Oh, there is one unsignificant thing. It is know that that the
variable in y[n] will allways equal 0 if ANDed.
so, do i have to write a function which i have to call constantly, or
is there a smarter way?
NB. the y[n] variable must of course not be altered in anyway by this
operation.

You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.

The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -
i dont quite understand your last sentence, maybe a typo? but i do
understand your message, and let me point out that im only looking for
smart solutions here.

@code
union T {
struct {
uint64 y[n];
};
uint64 x;
};

This is of course madness, but it would be a very simple solution if
thing worked different.
I am not expecting anything like this, but i am hoping as 'n' is a
somewhat high number. (less than 64 of course)

Sep 18 '07 #3
za******@gmail.com wrote:
On 18 Sep., 22:57, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>zacar...@gmail.com wrote:
>>typedef unsigned long long uint64 // just so we dont get confused ;)
>>ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all
times contain a value equal to the ORing of all the n variables. eg.
>>@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code
>>i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i
think). Oh, there is one unsignificant thing. It is know that that
the variable in y[n] will allways equal 0 if ANDed.
>>so, do i have to write a function which i have to call constantly,
or is there a smarter way?
>>NB. the y[n] variable must of course not be altered in anyway by
this operation.

You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.

The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul
tekst i anførselstegn -

- Vis tekst i anførselstegn -

i dont quite understand your last sentence, maybe a typo?
Nope, no typo AFAICS. What don't you understand?
but i do
understand your message, and let me point out that im only looking for
smart solutions here.
Although I've been known to provide stupid solutions on a rare occasion,
I don't think the one I outlined here qualifies.
>
@code
union T {
struct {
uint64 y[n];
};
uint64 x;
};

This is of course madness, but it would be a very simple solution if
thing worked different.
It would be, yes.
I am not expecting anything like this, but i am hoping as 'n' is a
somewhat high number. (less than 64 of course)
You have not specified what values your 'y' variables could take but
if they are purely bits, you could look into bit fields:

union T {
struct {
uint64 y0 : 1;
uint64 y1 : 1;
...
} y;

uint64 x;
};

Not sure how you progress from this to indexing within the 'y' member.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 18 '07 #4
On 18 Sep., 23:41, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
zacar...@gmail.com wrote:
On 18 Sep., 22:57, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
zacar...@gmail.com wrote:
typedef unsigned long long uint64 // just so we dont get confused ;)
>ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all
times contain a value equal to the ORing of all the n variables. eg.
>@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code
>i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i
think). Oh, there is one unsignificant thing. It is know that that
the variable in y[n] will allways equal 0 if ANDed.
>so, do i have to write a function which i have to call constantly,
or is there a smarter way?
>NB. the y[n] variable must of course not be altered in anyway by
this operation.
You might want to wrap your 'y' in a class. Implement it so that it
gives access to its internal data array using, say, overloaded op[]
which should return some kind of proxy thing (so you can overload its
assignment operator). Then every time you assign to an element in
that "array", update some member 'x'. The outside 'x' should simply
be a reference to the updated 'x' in the class.
The update is not going to "happen automatically", you still have to
create some code to do it, but it will only happen if any of the
values get assigned to, which is basically what you want, right?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul
tekst i anførselstegn -
- Vis tekst i anførselstegn -
i dont quite understand your last sentence, maybe a typo?

Nope, no typo AFAICS. What don't you understand?
but i do
understand your message, and let me point out that im only looking for
smart solutions here.

Although I've been known to provide stupid solutions on a rare occasion,
I don't think the one I outlined here qualifies.
@code
union T {
struct {
uint64 y[n];
};
uint64 x;
};
This is of course madness, but it would be a very simple solution if
thing worked different.

It would be, yes.
I am not expecting anything like this, but i am hoping as 'n' is a
somewhat high number. (less than 64 of course)

You have not specified what values your 'y' variables could take but
if they are purely bits, you could look into bit fields:

union T {
struct {
uint64 y0 : 1;
uint64 y1 : 1;
...
} y;

uint64 x;
};

Not sure how you progress from this to indexing within the 'y' member.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -
y[n] containt only either one or none set bits, but i need to know the
"position" of the set bit in the variable. You argue that bitsets
would be worth looking at and i have thought about it, but they are
just not suited for my needs.

I am playing around with bitboards, and at first it seems that bitsets
are the perfect candidate, but that is not the case, at least not for
this.

Sep 18 '07 #5
On 18 Sep., 23:57, zacar...@gmail.com wrote:
On 18 Sep., 23:41, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:


zacar...@gmail.com wrote:
On 18 Sep., 22:57, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>zacar...@gmail.com wrote:
>>typedef unsigned long long uint64 // just so we dont get confused ;)
>>ok, the problem is this.
>>I have 1 + n number of uint64 variables and the 1 should at all
>>times contain a value equal to the ORing of all the n variables. eg.
>>@code
>>uint64 x;
>>uint64 y[n];
>>x = y[0] | y[1] | y[2] ...| y[n-1]
>>@code
>>i would like this to happen automaticly, like x pointing to severel
>>variables at the same time, which of course is not possible (i
>>think). Oh, there is one unsignificant thing. It is know that that
>>the variable in y[n] will allways equal 0 if ANDed.
>>so, do i have to write a function which i have to call constantly,
>>or is there a smarter way?
>>NB. the y[n] variable must of course not be altered in anyway by
>>this operation.
>You might want to wrap your 'y' in a class. Implement it so that it
>gives access to its internal data array using, say, overloaded op[]
>which should return some kind of proxy thing (so you can overload its
>assignment operator). Then every time you assign to an element in
>that "array", update some member 'x'. The outside 'x' should simply
>be a reference to the updated 'x' in the class.
>The update is not going to "happen automatically", you still have to
>create some code to do it, but it will only happen if any of the
>values get assigned to, which is basically what you want, right?
>V
>--
>Please remove capital 'A's when replying by e-mail
>I do not respond to top-posted replies, please don't ask- Skjul
>tekst i anførselstegn -
>- Vis tekst i anførselstegn -
i dont quite understand your last sentence, maybe a typo?
Nope, no typo AFAICS. What don't you understand?
but i do
understand your message, and let me point out that im only looking for
smart solutions here.
Although I've been known to provide stupid solutions on a rare occasion,
I don't think the one I outlined here qualifies.
@code
union T {
struct {
uint64 y[n];
};
uint64 x;
};
This is of course madness, but it would be a very simple solution if
thing worked different.
It would be, yes.
I am not expecting anything like this, but i am hoping as 'n' is a
somewhat high number. (less than 64 of course)
You have not specified what values your 'y' variables could take but
if they are purely bits, you could look into bit fields:
union T {
struct {
uint64 y0 : 1;
uint64 y1 : 1;
...
} y;
uint64 x;
};
Not sure how you progress from this to indexing within the 'y' member.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Skjul tekst ianførselstegn -
- Vis tekst i anførselstegn -

y[n] containt only either one or none set bits, but i need to know the
"position" of the set bit in the variable. You argue that bitsets
would be worth looking at and i have thought about it, but they are
just not suited for my needs.

I am playing around with bitboards, and at first it seems that bitsets
are the perfect candidate, but that is not the case, at least not for
this.- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -
i read wront, you didnt say bitsets but bitfiels, sorry bout that.
anyway i cant use it.

Sep 18 '07 #6
za******@gmail.com wrote:
typedef unsigned long long uint64 // just so we dont get confused ;)

ok, the problem is this.
I have 1 + n number of uint64 variables and the 1 should at all times
contain a value equal to the ORing of all the n variables. eg.

@code
uint64 x;
uint64 y[n];
x = y[0] | y[1] | y[2] ...| y[n-1]
@code

i would like this to happen automaticly, like x pointing to severel
variables at the same time, which of course is not possible (i think).
Oh, there is one unsignificant thing. It is know that that the
variable in y[n] will allways equal 0 if ANDed.

so, do i have to write a function which i have to call constantly, or
is there a smarter way?

NB. the y[n] variable must of course not be altered in anyway by this
operation.
Option 1:
You may be able to get away with an O(log N) algorithm every time you
change an element in Y if you store intermediate values in an array.

Option 2:
Alternatively for O(1) complexity, you can keep counts on number of bits
set for each bit position. When a value is changed, you either
increment or decrement the count for a bit position, if any of them go
to zero then you clear the respective bit, it it is incremented you set
the respective bit.

Option 2 can also be improved to be O(number of bits changed) with a
little use of bit twiddling.
Sep 18 '07 #7
za******@gmail.com wrote:
....
y[n] containt only either one or none set bits,
You don't need a bitset.

All you need to know is bit position and y[] position.

If you only have 64 bit positions, then you don't need to actually store
y[], only bit index and a "not set" value (probably -1) and so you can
use an unsigned char for your bit index.

e.g.

struct BitThing
{
struct Data
{
uint64 x;
signed char ypos[ N ];
};

Data m_stuff;

Bithing()
: m_stuff()
{
}

...

class BithingProxy // proxy for operator []
{
...
};

BithingProxy operator[]( unsigned y_index )
{
return BithingProxy( this, y_index );
}

const uint64 operator[]( unsigned y_index ) const
{
assert( y_index < N );
return m_stuff.ypos[ y_index ] == -1 ? 0 :
uint64(1)<< m_stuff.ypos[y_index];
}

};

BithingProxy will allow you to write:

------
Bithing x;
x[5] = 1<<4;
------
.... just need to fill in the magic.
Sep 18 '07 #8
On 19 Sep., 00:37, Gianni Mariani <gi3nos...@mariani.wswrote:
zacar...@gmail.com wrote:

...
y[n] containt only either one or none set bits,

You don't need a bitset.

All you need to know is bit position and y[] position.

If you only have 64 bit positions, then you don't need to actually store
y[], only bit index and a "not set" value (probably -1) and so you can
use an unsigned char for your bit index.

e.g.

struct BitThing
{
struct Data
{
uint64 x;
signed char ypos[ N ];
};

Data m_stuff;

Bithing()
: m_stuff()
{
}

...

class BithingProxy // proxy for operator []
{
...
};

BithingProxy operator[]( unsigned y_index )
{
return BithingProxy( this, y_index );
}

const uint64 operator[]( unsigned y_index ) const
{
assert( y_index < N );
return m_stuff.ypos[ y_index ] == -1 ? 0 :
uint64(1)<< m_stuff.ypos[y_index];
}

};

BithingProxy will allow you to write:

------
Bithing x;
x[5] = 1<<4;
------

... just need to fill in the magic.

This way is possible i guess, but severen modifications is need.
1. i would still need to be able to treat y as an uint64 as i would
need the ability to perform various bitwise opperations on it.
I supose you could do something like this:

struct T {
unsigned char pos:6;
unsigned char y:1;
T():pos(0),t(0) {}
T(uint64 n) {
for(int i = 0; pos == 0; i++) {
if(n&(1ull<<i) == 1) {
pos = i;
y = 1;
}
}
}
};

allso there would need to be implemented alot of ostream, istream,
opprator, stuff that i know nothing about...
Sep 18 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bryan Parkoff | last post by:
I create one 32 Bits variable and four pointer variables. Four pointer variables link or point to one 32 Bits variable. Each pointer variable is 8 Bits. Look at my example below. unsigned int...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
23
by: Eric J.Hu | last post by:
Hi, I have following code, want do pointer convert. It always complain: vcnvt.c: In function `main': vcnvt.c:20: warning: dereferencing `void *' pointer vcnvt.c:20: request for member `key'...
3
by: Rennie deGraaf | last post by:
Let's say that I want to read a method pointer in from a stream. (I'm not saying that this is a good design idea, or that I actually have a reason to do this.) If I wanted to read in a function...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
10
by: kkirtac | last post by:
Hi, i have a void pointer and i cast it to an appropriate known type before using. The types which i cast this void* to are, from the Intel's open source computer vision library. Here is my piece...
12
by: Nyang A. Phra | last post by:
I have following code: union Pointer { void *objp; void (*funcp)(); }; Pointer p = ... ; ....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.