logo
Tags down

shadow

Adding bits at specific indexes for a uint8_t block


By : Shahbaz Jutt
Date : September 23 2020, 12:00 PM
Hope this helps So I have a pointer to a uint8_t array in the form of: , Say you start with the following:
code :
src[0]   src[1]   src[2]   src[3]   src[4]   src[5]   src[6]   src[7]
-------- -------- -------- -------- -------- -------- -------- --------
       7        6        5        4        3        2        1
76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210
aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff gggggggg hhhhhhhh
dst[0]   dst[1]   dst[2]   dst[3]   dst[4]   dst[5]   dst[6]   dst[7]   dst[8]
-------- -------- -------- -------- -------- -------- -------- -------- --------
       1
       0        7        6        5        4        3        2        1
76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210 76543210
aaaaaaa0 abbbbbbb bccccccc cddddddd deeeeee0 eeffffff ffggggg0 ggghhhh0 hhh0h000
dst[0] = src[0] & 0xFE;
dst[1] = ((src[0]       ) << 7) | ((src[1]       ) >> 1);
dst[2] = ((src[1]       ) << 7) | ((src[2]       ) >> 1);
dst[3] = ((src[2]       ) << 7) | ((src[3]       ) >> 1);
dst[4] = ((src[3]       ) << 7) | ((src[4] & 0xFC) >> 1);
dst[5] = ((src[4]       ) << 6) | ((src[5]       ) >> 2);
dst[6] = ((src[5]       ) << 6) | ((src[6] & 0xFC) >> 2);
dst[7] = ((src[6]       ) << 5) | ((src[7] & 0xFC) >> 3);
dst[8] = ((src[7] & 0x0E) << 5) | ((src[7] & 0x01) << 3);


Share : facebook icon twitter icon

uint8_t - 8 bits guarantee


By : atomJ
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further How & where is it guaranteed that an uint8_t is 8-bits? , From C++:

Given an uint8_t, find out how many bits are set


By : user3461881
Date : March 29 2020, 07:55 AM
should help you out Use a builtin:
code :
int bits_set(uint8_t x) {
    return __builtin_popcount(x);
}
int bits_set(uint8_t x) {
    // easy enough to generate this
    static const uint8_t table[] = {0, 1, 1, 2, 1, 2, ... };

    // then it's just a lookup
    return table[x];
}

how to set 3 lower bits of uint8_t in C


By : Benjamin Barber
Date : March 29 2020, 07:55 AM
should help you out I would like to set the 3 lower bites of uint8_t with value 3. I've tried the following: , To set bits you need |:
code :
temp = temp | 0x3;
temp |= 0x3;
temp &= ~0x7; // clear bits
temp |= 0x3;  // set bits

Are uint8_t , uint16_t guaranteed to be of the 8 and 16 bits respectively?


By : Moheen_Rashid
Date : March 29 2020, 07:55 AM
To fix this issue No, those data types are not guaranteed to be 8 or 16 bits. If they exist, they are guaranteed to be 8 or 16 bits, but they are not required to exist.
In your case, use uint_least8_t and uint_least16_t. Those are required to exist, and will be the smallest type with at least that number of bits.

How to put a uint8_t in the middle of uint16_t without affecting other bits


By : Nino Leo
Date : March 29 2020, 07:55 AM
will be helpful for those in need You are correct about the use of the two operations | and &, but you are not using them in the right order, and you are not positioning one of the operands correctly.
Here is the correct way of doing it:
code :
*mcp &= ~MCP4901_SPI_MASK_DATA; // Clear out the middle bits
*mcp |= ((uint16_t)value) << 4; // Put in the value in the middle
*mcp = (*mcp & ~MCP4901_SPI_MASK_DATA) | (((uint16_t)value) << 4);
Related Posts Related Posts :
  • Why do these two execvp produce different results?
  • Pass uintmax_t or size_t to custom printf conversion specifier
  • Why does free() leaves stuff in memory?
  • Why pointers can't be used to index arrays?
  • memory allocation eror in C
  • C custom datatypes mapped to C datatypes grouped under a single struct
  • pipe() data is not transferred to child process
  • Getting a core dump from a simple C program
  • Fatal error on makefile, need to understand the problem
  • How can I add a delay of 90 minutes when a port has gone from 0 to 1?
  • To use strcpy or not
  • the usage of strtok() in c shows warnings and returns segmentation fault(core dumped)
  • Trouble allocating array of structs
  • Only first char of user input used in array
  • Why does "int x = 5; printf("%d %d %d", x==5, x=10, x==5);" in C print "0 10 0"?
  • How to scan specific string format in C?
  • sscanf skipping the final value when reading multiple values from a line
  • How can I access full memory space in FreeDOS with C application
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • Passing a generic argument in a C function
  • Restoring stdout after redirecting it via freopen()
  • Tuples "cannot be marshalled in a foreign call" when trying to export a Haskell function to C
  • Dereferencing double pointer to pass to recursive function
  • Why is the output different in these two scenarios
  • what will happen if we dont use free() for allocated memory
  • counting words from user input
  • Why am I not getting the concatenated string?
  • not understandable pointers errors
  • Issue with function memory allocation
  • /usr/bin/ld: cannot find -lioutil in Makefile
  • Segmentation Fault when finding longest word in input
  • undefined reference to `palloc'
  • DMA transfer taking more time than CPU transfer
  • How can I make my code find solution while working at specific coordination?
  • How to fix implicit declaration of function?
  • Writing a file line by line in C
  • "accept" always returns 1, but no descriptor has been closed
  • How to fix 'Bad file descriptor' after implementing here-string redirection in my own shell
  • Why this output string to number?
  • Is there any way to make this adding an infinite?
  • for loop is iterating printf written after scanf only once
  • How to efficienty count from 0000 to 9999 in a digit display?
  • Implicit conversion and explicit conversion in c
  • Attempting to convert a value into 2s complement in C
  • else statement is resulting in identifier expected in c 13
  • Thread-safety vs atomicity in C
  • Segmentation fault - Trying to read binary file into memory
  • How to make a variable declared in constructor visible in the main file
  • What is the fastest way to reverse a power of two in C?
  • Reading and writing structure to binary file in C, then print structure elements
  • Why casting double to int might give different results?
  • Greedy algorithm in C not returning any values?
  • How to find base address of same char in string array using pointers in C language?
  • Quick question, why is scanf_s throwing an exception in runtime here? I am very confused
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk