0

I'm trying to get my program to work where a certain bit is being flipped. I have this function called flipbit(int *p, int m). The user needs to input a digit and a bit number. Let's say the user gives the number 8 (00001000) and the bit number 2, so the 2nd bit in 8 should be flipped, which becomes 00001010. How can I program this?

EDIT: I made a stupid mistake, I want to count starting from 0, so the 2nd bit in 8 flipped is actually 00001100 instead of 00001010.

#include <stdio.h>
#include <stdlib.h>

void flipbit(int *p, int m) {
    int digit;
    digit = *p;

    int bit;
    bit = &m;

    int result;

    //printf("The numbers are %d %d", digit, bit);

    printf("%d", result);
}

int main() {
    int number1;
    int number2;

    printf("Give number and bit: ");
    scanf("%d, %d",&number1, &number2);

    flipbit(&number1, &number2);

    return 0;
}
3
  • 1
    This question has an answer that might be helpful: stackoverflow.com/questions/47981/… Commented Oct 17, 2016 at 13:02
  • Here's a tip: when developing code, always compile with warnings enabled (e.g., add -Wall -pedantic to the command line options). Doing so would have alerted you to the incorrect handling of the second argument to your function.
    – r3mainer
    Commented Oct 17, 2016 at 13:16
  • Please don't update your code with suggested changes. That invalidates the existing answers.
    – dbush
    Commented Oct 17, 2016 at 14:30

2 Answers 2

3

Given the bit to flip, you first need to create a mask. Do that by taking the value 1 and left shifting it by the bit number.

Once you have that mask, use the bitwise XOR operator ^ to flip the bit.

int mask = 1 << m;
*p = *p ^ mask;
3
  • Thanks for your answer, but shouldn't it be digit = digit ^ mask;?
    – Teuntje
    Commented Oct 17, 2016 at 13:21
  • 2
    @Teuntje Not if you want the result to be visible from the calling function.
    – dbush
    Commented Oct 17, 2016 at 13:24
  • a more compact form is value ^= (1 << bit_number); Commented Oct 19, 2016 at 6:35
0

When checking your source code, there are mixture between pointer and value. To comply the call of your flipbit() function with the declaration, the call should be:

// first parameter is a pointer and second parameter is a value
flipbit(&number1, number2); // void flipbit(int *p, int m);

Inside the flipbit() function, the mixture continues because digit is a value and p is a pointer. The code should be:

int digit;

// 'digit' is a value and 'p' is a pointer
digit = p[0]; // 'digit' is the first value pointed by 'p'

Same error kind of error with the 'bit' parameter

int bit;

// 'bit' is a value and 'm' is a value
bit = m;

And the result to flip a bit is the XOR operation.

the bit number 2, so the 2nd

Due to your specification, you have to shift only of (bit - 1). So, in your case: 0x0001(or 0000.0000.0000.0001b) << (2 - 1) = 0x0002(or 0000.0000.0000.0010b)

result is 0x0010(or 0000.0000.0000.1000b) XOR 0x0002(or 0000.0000.0000.0010b) = 0x0012(or 0000.0000.0001.0010b).

int result;

result = digit ^ (0x0001 << (bit - 1));

Did you enter '9, 1' to comply with the scanf("%d, %d",..) ?

6
  • Why does bit have -1 infront?
    – Teuntje
    Commented Oct 17, 2016 at 13:36
  • Thanks for the edit, I'm trying to run the program. I'm giving the input 9 1, which should give me the output 11, but it gives me output 8. What am I doing wrong? I have updated the code.
    – Teuntje
    Commented Oct 17, 2016 at 14:00
  • If you do this: result = digit ^ (1 << (bit));, bit number should be in the range 7 .. 0 for 8 bits. Commented Oct 17, 2016 at 14:13
  • @FloppySoftware I'm getting this output: -2147483648
    – Teuntje
    Commented Oct 17, 2016 at 14:29
  • @Piquard, yes to comply with the scanf. I made a mistake, check my EDIT.
    – Teuntje
    Commented Oct 17, 2016 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.