#### Archived

This topic is now archived and is closed to further replies.

# What does &~0x0F mean?

This topic is 5408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was reading the newest sweet snippet "SSE2 for Dummies (who know C/C++)" at http://gamedev.net/reference/articles/article1987.asp, and I need a little help understanding the following function please: inline void *AllignData(void *data) { return (void *)(((int)data + 15) &~ 0x0F); } Is &~0x0F the expression for the start of a memory block??? I am assuming from the article that this function adds 15 bytes to the address of data from the start of the memory block. Is this correct? Thanks.

##### Share on other sites
data is the pointer you want to align

doing "return (void *)((int)(data) &~0x0f))" will align the pointer
to the nearest place where it divides by 16.
0x0f = 15 , or in binary 00000000000000000000000000001111 (hope its 32 bit),now ~ox0f is 11111111111111111111111111110000
doing a (void *)((int)(data) & 0~0xf)) that is logical and , will just clear the 4 lowest bits and and that pointer is aligned.
one problems is that you doing want the pointer to be aligned backwards , because that will give you a pointer to a place you did not malloc, you want it to go only forward , that is align to the nearest 16 only in the forward direction , that why you need to add the +15 , this solved this little problem

##### Share on other sites
"&" is the binary AND operator. In the expression c = a & b, each bit in c is set according the corresponding bits in a and b using this table:
a b  c
- - -
0 0 0
0 1 0
1 0 0
1 1 1
"~" is the unary NOT operator. In the expression c = ~a, each bit in c is set according the corresponding bit in a using this table:
a  c
- -
0 1
1 0

~0x0F == 0xFFFFFFF0 for 32-bit values.

The function takes the value of the pointer data and returns the smallest address greater than or equal to data that is a multiple of 16. To get an idea of how it works, take a look at this table:

data data+15 (data + 15) & ~0x0F
---- ------- -------------------
0 15 00
1 16 16
2 17 16
3 18 16
4 19 16
5 20 16
6 21 16
7 22 16
8 23 16
9 24 16
10 25 16
11 26 16
12 27 16
13 28 16
14 29 16
15 30 16
16 31 16
17 32 32
18 33 32
... ... ...
31 46 32
32 47 32
33 48 48
34 49 48
... ... ...
47 62 48
48 63 48
49 64 64
50 65 64
... ... ...
63 78 64
64 79 64
65 80 80
66 81 80
... ... ...

[edited by - JohnBolton on September 2, 2003 7:15:08 PM]

##### Share on other sites
quote:
Original post by gonen
one problems is that you doing want the pointer to be aligned backwards , because that will give you a pointer to a place you did not malloc, you want it to go only forward , that is align to the nearest 16 only in the forward direction , that why you need to add the +15 , this solved this little problem

I suspose I should post this in the article discussion thread, but since you brought it up : You also have to add 15 to the size of the memory allocated, in the article he adds sixteen, by-way-of two additional doubles.

[edited by - Magmai Kai Holmlor on September 2, 2003 7:42:40 PM]

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 9
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999990
×