Please support our C advertiser: Programming Forums
Views: 2623 | Replies: 4
![]() |
•
•
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation:
Rep Power: 3
Solved Threads: 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

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) If I understand you.
1 left shifted 4 is 10000 (binary)
1100110 (binary) + 10000 (binary) ------- 1110110 (binary) = 118 decimal
#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
*/ High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode