error: invalid operands to binary &

Programming, for all ages and all languages.
Post Reply
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

error: invalid operands to binary &

Post by FlashBurn »

I get this error because I want to "and" a pointer to test if it is aligned on a 4kb pg.

Is there another way to do that? At the moment I do:

Code: Select all

if((*p & 0xfff) != 0)
//code
Which does not work :(
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

first of all, what language is this?
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

if it's c/c++ 2 "&"'s are used, like so:

Code: Select all

if((a == b) && (b == c))
{
    ...
}
But what you have doesn't seem valid:

Code: Select all

if((*p && 0xfff) != 0))
Might I suggest:

Code: Select all

if((p != 0) && (0xfff != 0))
{
    ...
}
I omitted the "*" becasue I', assuming "p" is a pointer anyway.
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

Post by FlashBurn »

I want to take the value of the pointer (the address) "and" it with 0xfff to look if the address is aligned!

There has to be a way how I can do this.

The language I use is "C"!
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: error: invalid operands to binary &

Post by Alboin »

Code: Select all

if(((*p) & 0xfff) != 0)
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Clearly 'eboyd' doesn't know anything about bitwise operations.. :lol:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

I think what you want is

Code: Select all

if (((uintptr_t)p & 0xFFF) != 0)
where p is the pointer (say a 'void*'), right?
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

Post by FlashBurn »

Yeah, but how do you define uintptr_t?

And my pointer is a ptr to a struct and then then pointer which is in the struct:

Code: Select all

struct test_t
{
 void *base;
};

struct test_t *p;

if((p->base & 0xfff) != 0)
 //code
If I make this:

Code: Select all

uint32_t temp;
temp= p->base;

if((temp &0xfff) != 0)
 //code
It does compile, but is it also working?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Code: Select all

struct test_t
{
 void *base;
};

struct test_t *p;

if((p->base & 0xfff) != 0)
 //code 
Are you trying to find out if the address of 'p' is page aligned, or if the address pointed to by 'base' is page-aligned?
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

FlashBurn wrote:Yeah, but how do you define uintptr_t?
Depends on your Compiler and on the architecture you are targeting. The only requirements of an uintptr_t are that
a) if you convert a pointer to a uintptr_t and back to a pointer you do not loose information
b) uintptr_t should be of integer type (so that you can do math with it)
For GCC + x86 or x86-64 'unsigned long' does the thing. On GCC + x86 'unsigned long' is 32bit and on GCC + x86-64 it is 64bit, so in every case it matches the size of a pointer.

@FlashBurn: Your last code snippet looks ok to me (But I would use uintptr_t for more portability). Just to clarify: C does not offer binary operations for pointers, so you have to cast the pointer to a type where you can do binary operations.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

bluecode wrote:
FlashBurn wrote:Yeah, but how do you define uintptr_t?
Depends on your Compiler and on the architecture you are targeting.
Actually, no. ;) You define uintptr_t like this:

Code: Select all

#include <stdint.h>
Well, at least if you're working in a C99 environment. ;)
Every good solution is obvious once you've found it.
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

Post by FlashBurn »

Ok, I solved it with a typcast to an uint32_t.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

Solar wrote:Actually, no. ;) You define uintptr_t like this:

Code: Select all

#include <stdint.h>
Well, at least if you're working in a C99 environment.
:roll:
Actually I was thinking he is using this in his OS and does not have a full C99 environment and has to provide his own C99 environment. :wink: But thanks for mentioning that what I said should happen within stdint.h.
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

Post by eboyd »

Clearly 'eboyd' doesn't know anything about bitwise operations.. Laughing
Thanks. I love being publicly ridiculed.

But you're right. I don't use bitwise operations. I didn't realize that's what his post was all about.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

bluecode wrote:Actually I was thinking he is using this in his OS and does not have a full C99 environment and has to provide his own C99 environment. :wink:
As he posted to "General Programming"... besides, it's hard to remember how much of an environment everyone's OS provides. For him, the solution was to typedef uint32_t... had I suggested that, it would have sounded a bit silly if he didn't have an uint32_t, right? 8)
Every good solution is obvious once you've found it.
Post Reply