What does (size + 7) & ~ 7 mean?

I am reading the multiboot2 specification. You can find it here . Compared to the previous version, he names all his “tags” of his structures. They are defined as follows:

3.1.3 General tag structure

Tags are a buffer of structures following one another, based on size u_virt. Each structure has the following format:

    + ------------------- +
u16 | type |
u16 | flags |
u32 | size |
    + ------------------- +

typedivided into 2 parts. The bottom one contains the identifier content of the rest of the tag. sizecontains tag size including header fields. If the 0of flags(also known as optional) bit is set, if the loader can ignore this tag, if it lacks the appropriate support. Tags end with a type tag 0and size 8.

Then in the following example:

for (tag = (struct multiboot_tag *) (addr + 8);
     tag->type != MULTIBOOT_TAG_TYPE_END;
     tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag
     + ((tag->size + 7) & ~7)))

The last part confuses me. In Multiboot 1, the code was much simpler, you could just make multiboot_some_structure * mss = (multiboot_some_structure *) mbi->some_addrand receive elements directly, without confusion with such code.

Can someone explain what it means ((tag->size + 7) & ~7)?

+4
source share
4 answers

chux, tag->size 8.

, .

, size 16:

 00010000         // 16 in binary
+00000111         // add 7
 --------
 00010111         // results in 23

~7 7 . :

 00010111         // 23 (from pervious step)
&11111000         // bitwise-AND ~7
 --------
 00010000         // results in 16

, size 17:

 00010001         // 17 in binary
+00000111         // add 7
 --------
 00011000         // results in 24

:

 00011000         // 24 (from pervious step)
&11111000         // bitwise-AND ~7
 --------
 00011000         // results in 24

, 3 size , .. 8, (size+7)&~7 , , . 1, , 8, , , 8.

+6

~ . , 16 :

7 - 0000 0000 0000 0111

~ 7 - 1111 1111 1111 1000

, 0 0. , 1, . ,

N 0 = 0

N 1 = N

, ~ 7, , .

+3

@chux . , size 8, . , 15bpp:

//+7/8 will cause this to round up...
uint32_t vbe_bytes_per_pixel = (vbe_bits_per_pixel + 7) / 8;

:

, 16bpp. 15bpp, RGB 5: 5: 5, u_int16 . , , 5- , 32 32786 (true 16bpp RGB 5: 6: 5, 65536 ). 16- RGB- - , .

+2
source

& ~7 sets the last three bits to 0

+1
source

Source: https://habr.com/ru/post/1612952/


All Articles