| | |
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 |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file floatingpointvalidation fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram inches input intmain() iso kernel keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf opendocumentformat openwebfoundation owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault sequential single socket socketprograming socketprogramming stack standard string systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






