Hello :)

i need to write a program which gets from user number and index
( 0 <= number <= 255 , 0<=index<=7) .
then to add up to the number in bitwise a 1 in the place of the index :
if number is 102 which is 01100110
and index is 4 which is 01110110
and get 118 as a result.

for some reason it doesnt do that ..... i have no idea why , i used OR operation between numbers ..... :rolleyes:

Code added

thanx , Yotam

102 (decimal) = 1100110 (binary)
1 left shifted 4 is 10000 (binary)

1100110 (binary)
+ 10000 (binary)
-------
1110110 (binary) = 118 decimal

If I understand you.

#include <stdio.h>

int main(void)
{
   int number = 102, index = 4;
   number += 1 << index;
   printf("number = %d\n", number);
   return 0;
}

/* my output
number = 118
*/

that code is actually adding 1 in place of index ?

ch2=pow(2,index + 1);

Why did you add 1 to the index? - 32 (2^5) in binary is 00100000 - The code would give your expected result without that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.