[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

What does it mean.

I don't understand this logic.

& = And (Bits)
~ = Not (Bits Too).

Well in a book I have this code.
iWidth = 2 * ((cx * cBitsPixel + 15) & ~15) >> 3;

What does & ~ mean?... I can figure out mixing the And and not operators...

And what does this code line do exactly?...
For example assuming that:
cx = 4, cBitsPixel = 5... What would be the result?.

Thanks.
0
Marsc
Asked:
Marsc
1 Solution
 
ZizzerCommented:
~15 means that the compiler will invert the bit pattern formed by the integer 15.  In hex, 15 is 0x000f (assuming 2-byte integers), so ~15 is 0xfff0.

This value is anded with 2*(cx*cBitsPixel+15).  The net effect is to turn off the last four bits.

So to take your entire example:

2 * ((cx * cBitsPixel + 15) & ~15) >> 3
== 2 * ((4 * 5 + 15) & ~15) >> 3
== 2 * (35 & ~15) >> 3
== 2 * (0x0023 & 0xfff0) >> 3
== 2 * 0x0020 >> 3
== 0x0040 >> 3
== 0x0008

-- Zizzer
0
 
abk102299Commented:
> I don't understand this logic.
>
> & = And (Bits)
> ~ = Not (Bits Too).

> Well in a book I have this code.
> iWidth = 2 * ((cx * cBitsPixel + 15) & ~15) >> 3;

> What does & ~ mean?...

Let me guess: your book is about some kind of bitmaps, graphical images or something. Let's separate the parts of expression to make some sense of it. I'll try to go step by step, but I'm omitting some trivial explanation going from one form of the expression to another equivalent form.

1) cx         - x coordinate in pixels
2) cBitsPixel - amount of bits per pixel
3) cx * cBitsPixel - amount of bits to represent cx pixels

Ok, but in memory we store a row of bits in computer words. This particular example refers to 16-bit words. So, how many 16-bit words do we need to store cx pixels?

4) (cx * cBitsPixel + 15) / 16

And how many bits could be stored in these words?

4) (cx * cBitsPixel + 15) / 16 * 16
5) (cx * cBitsPixel + 15) >> 4 << 4
6) (cx * cBitsPixel + 15) & 0xfffffff0
7) (cx * cBitsPixel + 15) & (~0xf)
8) (cx * cBitsPixel + 15) & ~15

And how many bytes in these words?

8) ((cx * cBitsPixel + 15) & ~15) / 8
9) ((cx * cBitsPixel + 15) & ~15) >> 3

Now , I don’t know exactly why this amount of bytes has the prior multiplier 2. There could be various reason for this. What does your book says?

> I can figure out mixing the And
> and not operators...

> And what does this code line
> do exactly?...

I would say the code above evaluates the count of bytes necessary to storage graphical image row of cx pixels ( multiplied by 2 )

> For example assuming that:
> cx = 4, cBitsPixel = 5...
> What would be the result?.

2 * (4 * 5 + 15) / 16 * 16 / 8 ==
2 * (35) / 16 * 16 / 8 ==
2 * 2 * 16 / 8 ==
2 * 32 / 8 == 2 * 4  == 8

You could check this with the following code:

int main()
{
      int cx = 4, cBitsPixel = 5;
      printf("result: %d\n",2 * ((cx * cBitsPixel + 15) & ~15) >> 3);
      return 0;
}

the program outputs:

result: 8

Finally, I don't think this is too efficient expression neither from programming nor from educational point of view.
0
 
MarscAuthor Commented:
Well...

At first... I'm sorry for the delay... I was busy trying to solve another programming problems... but here I am

I have to accept zizzer's answer... 'cause it is good...
thanks...

abk:

Thanks for your comment...
Yes... of course that part of the book talk about bitmaps... The book is (as you can guess) Charles Petzold's "Programming Windows".

Well... I'm having some mathematical problems in my brain :-)

The book says something like this in the page 663:

---------------------------------------
iWidthBytes = 2 * ((cx * cBitsPixel + 15) / 16);

or as a C programer might tend to write it,

iWidth = (cx * cBitsPixels + 15) & ~15) >> 3;
----------------------------------------
The second way to get the iWidth is clear... but in the first way... I can't accomplish the same result.
Can you explain it to me?...

Well... I think I should to post some points for you... But please... explain it :-)







0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now