-4

The body of the function set_bit(uint64_tx, int pos, bool value) which returns the modified value of the input x, where the bit at the pos position is replaced with the value value.

Remember that in the C language (this is defined in stdbool.h), true is the integer 1 while false is the integer 0.

Code

uint8_t a=0b00000000;
uint8_t b=0b00001000;
uint8_t c=0b11111101;
uint8_t d=0b11011011;

// l'opération  ~( a ) renvoi 0b11111111
// l'opération (c & a) renvoi 0b00000000
// l'opération (c & b) renvoi 0b00001000
// l'opération (a | b) renvoi 0b00001000
// l'opération (d & c) renvoi 0b11011001  

#include <stdint.h>
#include <stdbool.h>

/*
* @pre 0<= pos < 64
*/

uint64_t set_bit(uint64_t x, int pos, bool value)
{
    // à compléter
}
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
  • 1
    Please ask only in English on this site. You can edit your question to replace the current text with a translation to English. – walnut Nov 26 '19 at 01:14

2 Answers2

0
uint64_t set_bit(uint64_t x, int pos, bool value)
{
    // check range
    if (pos<0 || (pos&0x40))
      return 0; // error
    return ((x &~((uint64_t)1<<pos)) | ((uint64_t)value<<pos));
}
0
uint64_t set_bit(uint64_t x, int pos, bool value)
{
    if(pos < 0 || pos > 64)
    {
        return x;
    }

    if(value)
    {
        return x | (((uint64_t)1) << (pos - 1));
    }
    else
    {
        return x & (~(((uint64_t)1) << (pos - 1)));
    }
}
user2859193
  • 434
  • 3
  • 11