0

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

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

void main()
{
unsigned char ch1,ch2;
int index,num,result;

printf("Please Enter an Unsigned Char and Index of Inclination\n");
scanf("%d%d",&num,&index);
   if( (num<0)||(num>255)||(index<0)||(index>7) )
   {
      printf("error\n");
      exit(1);
   }
ch1=num;
ch2=pow(2,index+1);
result=ch1|ch2;
printf("\n %d",result);

}
3
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by Bench
0

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
*/
0
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.