简体   繁体   中英

How do I print out all 8 bytes of 8 byte variables and how can I apply the `&&` bit wise operator on them?

How do I print out all 8 bytes of 8 byte variables and how can I apply the && bit wise operator on them ?

int main(void)
{
     uint64_t a = 0x2345678987654321;
     uint64_t b = 0x2345678987654321;
     uint64_t c = 0x11111111;
     b= 0x2345678987654321 && 0xFFFFFFFFFFFFFF00;
     printf("a is %llX b is %llX c is %llx" ,a, b, c);
     return EXIT_SUCCESS;
}

The && operator is a logical and. It yields a boolean (true/false, in C represented by an int value of 1 / 0 guaranteed) result iff both operands are non-zero. It also evaluates strictly left operand first and the right only iff the result depends on it (ie if the left operand is non-zero; this works correspondingly for a chain of identical logical operators).

You need the bit-wise & (and) operator which evaluates all (integer) operands:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>  // format-strings for stdint.h types

int main(void)
{
    uint64_t a = 0x2345678987654321U;
    uint64_t b = 0x2345678987654321U;
    uint64_t c = 0x11111111U;
    b = b & 0xFFFFFFFFFFFFFF00U;
    printf("a is %016" PRIx64 " b is %016" PRIx64 " c is %016" PRIx64, a, b, c);
}

Note I added U suffix to the constants to ensure they are unsigned. You might add a width suffix, but that is not required and actually quite useless, because LL , making the constant long long would not guarantee 64 bits (it could well be >64 bits). If you want constants with a specific type, cast them, eg:

#define U64_CONST ((uint64_t)0x123U)

An alternative way to get at least a 64 bit unsigned type:

#define U64_CONST UINT64_C(0x123U)

This macro will generate a uint_least64_t (see `stdint.h for more). As always, enable warnings to let your compiler help you finding constants which do not fit into the given type.

For similar reasons, the correct printf (see the link why I added 016 ) and scanf format-specifiers for stdint.h types are in inttypes.h . Each is a macro with a string-literal containing the correct format-specifier for your platform. Use string-literal concatenation (two adjacent string-literals are combined to a single string-literal - note there is no operator in-between) to assemble the format-string.

Note: The above is - of course - also valid for types of other bit-sizes (and possible more important).

You need to add a field width and zero padding flag to the %llX format specifier:

printf("a is %016llX b is %016llX c is %016llx" ,a, b, c);

The 16 says the field should be 16 characters wide (16 hex digits for a 64 bit number), and the leading 0 says that field should be padded on the left with zeros.

Also, && is the logical AND operator. The bitwise AND operator is & .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM