-3
int setBit7to1(int v)
{
    // TODO: set bit 7 to 1
    return v;
}

int setBit5to0(int v)
{
    // TODO: set bit 5 to 0
    return v;
}

for example

  • setBit7to1: if input: 0x01, output: 0x81
  • setBit5to0: if input: 0xffff, output: 0xffef

can someone help?

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
kuroi
  • 3
  • 2

2 Answers2

0
int setBit7to1(int v)
{
    //here, 7th bit will be or'd with 1( (x | 1) == 1)
    return (v | 0x80);
}

int setBit5to0(int v)
{
    //here, every bit except the 5th bit will be set to itself( (x ^ 1) == x ), 5th bit cleared
    return (v & 0xef);
}
schoolo
  • 61
  • 1
  • 4
0

if you want to set one bit to 1 just use this "bit | 1"(because 0 | 1 = 1;1 | 1 =1)

if you want to set one bit to 0 just use this "bit & 0"(because 0 & 0 = 0;1 & 0 = 0) So,for your question,you can write this

int setBit7to1(int v)
{
    return (v | 0x80);
}
int setBit5to0(int v)
{
    return (v & 0xffffffef);//If int is 4 BYTE
}
Leo
  • 24,596
  • 11
  • 71
  • 92