ApelPro

binMaskedValue

Jun 11th, 2021
593
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. uint16_t valueMask( uint16_t old, uint16_t value, uint16_t bits, uint16_t start ) {
  5.         uint16_t _m = ( ( ( 1 << bits ) - 1 ) << start );
  6.         uint16_t mask = 65535 ^ _m;
  7.         uint16_t ret = old;
  8.         ret = ret & mask;
  9.         ret = ret | ( value << start );
  10. }
  11.  
  12. void dec2bin( uint16_t bin ) {
  13.         uint16_t mask = ( 1 << 15 );
  14.         uint8_t i = 15;
  15.         while( mask != 0 ) {
  16.                 printf( "%hhu", ( bin & mask ) >> i-- );
  17.                 mask = mask >> 1;
  18.         }
  19.         printf( "\n" );
  20. }
  21.  
  22. int main() {
  23.  dec2bin( valueMask( 65535, 10, 4, 5 ) );
  24.  dec2bin( valueMask( 65535, 15, 8, 0 ) );
  25.  return 0;
  26. }
  27.  
  28.  
RAW Paste Data