| | |
bitwise operations in C
![]() |
•
•
Join Date: Jan 2006
Posts: 38
Reputation:
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)
C Syntax (Toggle Plain Text)
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
*/ "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
C Syntax (Toggle Plain Text)
ch2=pow(2,index + 1);
00100000 - The code would give your expected result without that. ![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Help! C program w/Pointer Notation to Convert Text
- Next Thread: sytem woes
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling segmentationfault send shape socket socketprograming socketprogramming stack standard string suggestions systemcall test unix urboc user voidmain() wab win32api windows.h






