0x14. C Bit manipulation Task 03 - humtej1204/holbertonschool-low_level_programming GitHub Wiki

3. 11

mandatory

Write a function that sets the value of a bit to 1 at a given index.

  • Prototype: int set_bit(unsigned long int *n, unsigned int index);
  • where index is the index, starting from 0 of the bit you want to set
  • Returns: 1 if it worked, or -1 if an error occurred
julien@ubuntu:~/0x14. Binary$ cat 3-main.c
#include <stdio.h>
#include "main.h"

/**
 * main - check the code
 *
 * Return: Always 0.
 */
int main(void)
{
    unsigned long int n;

    n = 1024;
    set_bit(&n, 5);
    printf("%lu\n", n);
    n = 0;
    set_bit(&n, 10);
    printf("%lu\n", n);
    n = 98;
    set_bit(&n, 0);
    printf("%lu\n", n);
    return (0);
}
julien@ubuntu:~/0x14. Binary$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-set_bit.c -o d
julien@ubuntu:~/0x14. Binary$ ./d
1056
1024
99
julien@ubuntu:~/0x14. Binary$ 

Repo:

  • GitHub repository: holbertonschool-low_level_programming
  • Directory: 0x14-bit_manipulation
  • File: 3-set_bit.c
⚠️ **GitHub.com Fallback** ⚠️