#### Archived

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

# What does &~0x0F mean?

## Recommended Posts

executor_2k2    122
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
gonen    122
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
JohnBolton    1372
"&" 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  00 1  01 0  01 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  11  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       001     16       162     17       163     18       164     19       165     20       166     21       167     22       168     23       169     24       1610    25       1611    26       1612    27       1613    28       1614    29       1615    30       1616    31       1617    32       3218    33       32...   ...      ...31    46       3232    47       3233    48       4834    49       48...   ...      ...47    62       4848    63       4849    64       6450    65       64...   ...      ...63    78       6464    79       6465    80       8066    81       80...   ...      ...

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

##### Share on other sites
Shannon Barber    1681
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]