1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <stdint.h> // included for uint8_t
void print_bits(uint8_t byte) {
for(int i = 0; i < 8; i++) {
printf("%d",((byte >> (7 - i)) & 1));
}
}
int main(int argc, char* argv[]) {
uint8_t byte = 0x00; // uint8_t is guaranteed to be unsigned and 8 bits (1 bytes)
// uint64_t is guaranteed to be unsigned and 64 bits (8 bytes)
// int8_t is guaranteed to be signed and 8 bits (1 byte)
byte = 0x01 << 2;
assert(byte == 0x04);
printf("byte == 0x%02x\n", byte);
// byte == ... obviously
// 0x ... because unlike mintf(…), printf(…) does not add the 0x prefix for hex
// %02x ... because %x would be just hex notation
// %2x would be hex notation, taking >= 2 positions, spaces on left
// %02x is the same but padding with zeros not spaces on left
//
print_bits(byte);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2023 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.