Created
April 11, 2024 20:34
-
-
Save eliaskanelis/a7b0ebc2910aeca76ba7b28b572d4efe to your computer and use it in GitHub Desktop.
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 <stdbool.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <assert.h> | |
static bool bitmask_writeU32( uint32_t *const address, const uint32_t mask, | |
const uint32_t pos, const uint32_t value ) | |
{ | |
const bool success = ( ( address != NULL ) && ( pos < 32U ) ); | |
if( success ) | |
{ | |
/* Clear the bits to be written */ | |
*address &= ~mask; | |
/* Write the value to the specified position */ | |
*address |= ( ( value << pos ) & mask ); | |
} | |
return success; | |
} | |
static bool bitmask_readU32( const uint32_t *const address, const uint32_t mask, | |
const uint32_t pos, uint32_t *const value ) | |
{ | |
const bool success = ( ( address != NULL ) && ( pos < 32U ) && | |
( value != NULL ) ); | |
if( success ) | |
{ | |
/* Extract the value */ | |
*value = ( *address & mask ) >> pos; | |
} | |
return success; | |
} | |
static void displayBinaryRepresentation( const uint32_t num ) | |
{ | |
printf( "0x" ); | |
for( uint32_t i = 31U; i != UINT32_MAX; i-- ) | |
{ | |
{ | |
printf( "%d", ( num >> i ) & 1 ); | |
} | |
} | |
printf( "\n" ); | |
} | |
static uint32_t findMaxValueFromBitmask( const uint32_t bitmask ) | |
{ | |
const uint32_t pos = __builtin_ctz( bitmask ); | |
const uint32_t maxValue = bitmask >> pos; | |
return maxValue; | |
} | |
static void testFun( const uint32_t value, const uint32_t bitmask, | |
const uint32_t pos ) | |
{ | |
uint32_t num = 0U; | |
uint32_t readValue = 0U; | |
bool success = bitmask_writeU32( &num, bitmask, pos, value ); | |
success &= bitmask_readU32( &num, bitmask, pos, &readValue ); | |
displayBinaryRepresentation( num ); | |
assert( value == readValue ); | |
assert( success == true ); | |
} | |
static bool isInConsecutiveSequences( const uint32_t num ) | |
{ | |
// Check if the number is of the form (2^n - 1) | |
// This condition checks if the number is a power of 2 minus 1 | |
return ( num & ( num + 1 ) ) == 0 && num != 0; | |
} | |
int main( void ) | |
{ | |
for( uint32_t bitmask = 0U; bitmask <= UINT32_MAX; bitmask++ ) | |
{ | |
if( !isInConsecutiveSequences( bitmask ) ) | |
{ | |
// Skip bitmasks that are not testable e.g: 0b101 | |
continue; | |
} | |
const uint32_t maxValue = findMaxValueFromBitmask( bitmask ); | |
const uint32_t pos = __builtin_ctz( bitmask ); | |
for( uint32_t value = 0U; value <= maxValue; value++ ) | |
{ | |
// printf( "Bitmask = %-5d Pos = %-5d Value = %-5d\n", bitmask, pos, value ); | |
testFun( value, bitmask, pos ); | |
} | |
} | |
( void )displayBinaryRepresentation; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment