c - Bit Twiddling Hacks -


can explain val stand in following example? totally have no idea value of val.

#define splat(p)       (*(p) * (~0ul / 255))  uint8_t *page; unsigned long val = splat(page); 

assume long 32 bits. ~0ul bit pattern of ones i.e. 0xffffffff. divide 255 or 0xff giving 0x01010101.

multiply 8 bit quantity , same byte 4 times; e.g 0x5a * 0x01010101 = 0x5a5a5a5a.

this works independent of size of long, bytes of long filled original byte.

for example 8 byte longs:

#include <stdio.h>  int main() {     printf("%lu\n", sizeof(long));     printf("%lx\n", (~0ul / 255));     return 0; } 

gives output:

8 101010101010101 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -