C ordening
This commit is contained in:
44
C/C(extra) ExtraAssignments/BitStuff/shared/bit_stuff.c
Normal file
44
C/C(extra) ExtraAssignments/BitStuff/shared/bit_stuff.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "bit_stuff.h"
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// leave resource_detector.h as last include!
|
||||
#include "resource_detector.h"
|
||||
|
||||
unsigned int count_ones(unsigned int value){
|
||||
unsigned int count = 0;
|
||||
while (value){
|
||||
count += value & 1;
|
||||
value >>= 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void make_bitmask(unsigned int width, unsigned int shift, unsigned int* mask){
|
||||
if (width == 0 || width > sizeof(unsigned int)*CHAR_BIT) return;
|
||||
if (width == sizeof(unsigned int) * CHAR_BIT) *mask = ~0U << shift;
|
||||
else *mask = ((1U << width) - 1) << shift;
|
||||
}
|
||||
|
||||
void apply_bitmask(unsigned int value, unsigned int mask, unsigned int* masked_value) {
|
||||
if (masked_value != NULL) {
|
||||
*masked_value = value & mask;
|
||||
}
|
||||
}
|
||||
|
||||
void flip_bit(unsigned int value, unsigned int bit_index, unsigned int* updated_value){
|
||||
|
||||
if (updated_value == 0 || bit_index >= sizeof(unsigned int) * CHAR_BIT) return;
|
||||
*updated_value = value ^ (1U << bit_index);
|
||||
}
|
||||
|
||||
void extract_nibbles_from_byte(uint8_t value, uint8_t* high_nibble, uint8_t* low_nibble){
|
||||
if (high_nibble == NULL || low_nibble == NULL) return;
|
||||
*high_nibble = (value >> 4) & 0xF;
|
||||
*low_nibble = value & 0xF;
|
||||
}
|
||||
|
||||
void combine_nibles_to_byte(uint8_t high_nibble, uint8_t low_nibble, uint8_t* value){
|
||||
if (value == NULL) return;
|
||||
*value = (high_nibble << 4) | (low_nibble);
|
||||
}
|
||||
36
C/C(extra) ExtraAssignments/BitStuff/shared/bit_stuff.h
Normal file
36
C/C(extra) ExtraAssignments/BitStuff/shared/bit_stuff.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* pre : -
|
||||
* post: the number of bits with value 1 is counted and returned
|
||||
*/
|
||||
unsigned int count_ones(unsigned int value);
|
||||
|
||||
/* pre : -
|
||||
* post: a bitmask with a given width and a given shift is generated (so w=5 and
|
||||
* s=1 gives 00111110)
|
||||
*/
|
||||
void make_bitmask(unsigned int width, unsigned int shift, unsigned int* mask);
|
||||
|
||||
/* pre : -
|
||||
* post: 'masked_value' is assigned the value of 'value' with the 'mask' applied
|
||||
*/
|
||||
void apply_bitmask(unsigned int value, unsigned int mask, unsigned int* masked_value);
|
||||
|
||||
/* pre : -
|
||||
* post: the bit of index 'bit_index' of 'value' is flipped: 0 --> 1, 1 --> 0.
|
||||
*/
|
||||
void flip_bit(unsigned int value, unsigned int bit_index, unsigned int* updated_value);
|
||||
|
||||
/* pre : -
|
||||
* post: the high and low nibbles of 'value' of stored in 'high_nibble' and
|
||||
* 'low_nibble'.
|
||||
*/
|
||||
void extract_nibbles_from_byte(uint8_t value, uint8_t* high_nibble, uint8_t* low_nibbe);
|
||||
|
||||
/* pre : -
|
||||
* post: the nibble values of the 'high_nibble' and 'low_nibble' are combined
|
||||
* and stored in 'value'
|
||||
*/
|
||||
void combine_nibles_to_byte(uint8_t high_nibble, uint8_t low_nibbe, uint8_t* value);
|
||||
Reference in New Issue
Block a user