-1

I tried finding this question, but all the other questions don't relate to my problem.

My issue: I have something like 0xFreeFoodU where I have to get specific positions and either flip them or make them 1s or 0s.

So for example, bits in position 2, 6, 10, 14, 18, 22 , 26, and 30 should be unchanged, whereas the bits in positions 3, 7, 11, 15, 19, 23, 27, and 31 should be changed to 1. I don't wanna post my entire prompt bc I don't wanna cheat and get someone else to do my hw for me. But giving me an answer to at least one of these will help millions.

This is bit manipulation. But I have no clue how to manipulate specific bits in specific positions. :(

EDIT I cant upload a full program; its too long. But I have a main function where I call the function I need. The function should just ideally have return and so-on; so far I have

return val_num ^ 0x22222222U; But I should be adding to it. I only want help with how to set certain bits to 1 and 0. Is masking required?

May Athena
  • 39
  • 8
  • 1
    Welcome to SO. Did you see this? https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit/47990#47990 Each operation in the top answer can be modified to affect multiple bits at once. – Gerhardh Nov 26 '18 at 10:07
  • What shall happen to the bits you didn't mention? – Gerhardh Nov 26 '18 at 10:07
  • Please also provide a [MCVE](https://stackoverflow.com/help/mcve) to show us where you are stuck – Gerhardh Nov 26 '18 at 10:09
  • Here I have a check bit function you might need. https://stackoverflow.com/questions/53301155/read-n-bit-from-a-byte/53301720#53301720 – Tsakiroglou Fotis Nov 26 '18 at 10:09
  • 1
    @gerhardh Oh, just switch some to zeroes and flip a few. I figured how to flip using XOR and place values. So I don't really wanna bother anyone with that. And I figure if I see what is done with the 1 conversions I can just apply to the 0 conversions. – May Athena Nov 26 '18 at 10:10
  • @ Tsakiroglou Fotis thank you! but that isnt what I need. We arent supposed to use arrays. – May Athena Nov 26 '18 at 10:13

2 Answers2

1

If you managed to do the XOR part, the part setting bits to 1 is nearly the same:

Create a mask with all bits:

unsigned int mask_0       = 0x....;      // bits that shall be set to 0

unsigned int mask_1       = 0x88888888;  // bits that shall be set to 1
// Note: this is 1<<3 | 1<<7 | 1<<11 ... All bits set that shall be changed

unsigned int mask_toggle  = 0x22222222;  // bits that shall be toggled
unsigned int value = 0xdeadbeef;

Perform required operation:

// toggle bits
value = value ^ mask_toggle;

// set bits to 1
value = value | mask_1;

// set bits to 0
value = value & ~mask_0;
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • Just for clarification, what are the 64s for? Do they signify certain bit positions or just stand for 1? – May Athena Nov 26 '18 at 10:25
  • And in case I didn't make it clear, thank you! This might be helpful, but I want first to get the concept before just copying your work ^^ – May Athena Nov 26 '18 at 10:25
  • How did you get to 0x22222222 for the toggle part? It's the same mechanism but a different operator – Gerhardh Nov 26 '18 at 10:28
  • Okay, let me try paraphrasing to see if I follow. So the 0x64646464 is just using place value then? Bc that is what I did for the 0x22222222U thing. Just used place value 2. So if the positions 0 thru 3 were 3696, it would flip 9. – May Athena Nov 26 '18 at 10:33
  • I don't understand your last sentence. – Gerhardh Nov 26 '18 at 11:14
  • just trying to use an example. I still do not follow where the 64 comes from. – May Athena Nov 26 '18 at 15:02
  • Ooooh. Haha story of my life. Thank you! It all makes more sense now! :D – May Athena Nov 28 '18 at 06:20
  • I would upvote your comment but I am a noob here and my vote doesnt count. But it counts in spirit! – May Athena Nov 28 '18 at 06:21
0

Use the bitwise OR operator (|) to set a bit.

number |= (1 << bitposition);

Use the bitwise AND operator (&) to clear a bit.

number &= ~(1 << bitposition);

The XOR operator (^) can be used to toggle a bit.

number ^= (1 << bitposition);

This is the program

#include<stdio.h>
void main()
{
        int a[] = { 3, 7, 11, 15, 19, 23, 27, 31},b = 1200,i;
        for(i=0;i<8;i++)
                b ^= (1 << a[i]);
        printf("%d\n",b);
}
  • Thank you for helping me! But I am really not supposed to use arrays. But I understand which concepts you wrote out clearly. This makes it easier in general to follow! :) – May Athena Nov 26 '18 at 10:34