Created
June 5, 2018 20:20
-
-
Save eduardoaugustojulio/284c566c1f1b168503cc4e6a92e9fe98 to your computer and use it in GitHub Desktop.
generate rand and make binary operations whit the result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8) | |
project(rand) | |
add_executable(${PROJECT_NAME} src/main.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c" | |
#define BYTE_TO_BINARY(byte) \ | |
(byte & 0x80 ? '1' : '0'), \ | |
(byte & 0x40 ? '1' : '0'), \ | |
(byte & 0x20 ? '1' : '0'), \ | |
(byte & 0x10 ? '1' : '0'), \ | |
(byte & 0x08 ? '1' : '0'), \ | |
(byte & 0x04 ? '1' : '0'), \ | |
(byte & 0x02 ? '1' : '0'), \ | |
(byte & 0x01 ? '1' : '0') | |
int main(const int argc, const char **argv){ | |
srand (time(NULL)); | |
int value = rand() % 4096; | |
printf("randomico %d\n", value); | |
unsigned char msb = (value & 0xff); | |
printf("Most signifcant byte %d\n",msb); | |
printf(BYTE_TO_BINARY_PATTERN"\n", BYTE_TO_BINARY(msb)); | |
unsigned char lsb = ((value >> 8) & 0xff); | |
printf("Last significant byte %d\n",lsb); | |
printf(BYTE_TO_BINARY_PATTERN"\n", BYTE_TO_BINARY(lsb)); | |
printf("bit_0 %d\n", (lsb & 0x01 ? 1 : 0 )); | |
printf("bit_1 %d\n", (lsb & 0x02 ? 1 : 0 )); | |
printf("bit_2 %d\n", (lsb & 0x04 ? 1 : 0 )); | |
printf("bit_3 %d\n", (lsb & 0x08 ? 1 : 0 )); | |
// porta.bit1 = (lsb & 0x01 ? 1 : 0 )); | |
// porta.bit2 = (lsb & 0x01 ? 1 : 0 )); | |
// porta.bit3 = (lsb & 0x01 ? 1 : 0 )); | |
// porta.bit4 = (lsb & 0x01 ? 1 : 0 )); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment