bitwise operations in C

Reply

Join Date: Jan 2006
Posts: 38
Reputation: YoTaMiX is an unknown quantity at this point 
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

bitwise operations in C

 
0
  #1
May 1st, 2006
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
Attached Files
File Type: c HW3_2.C (407 Bytes, 9 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: bitwise operations in C

 
0
  #2
May 1st, 2006
102 (decimal) = 1100110 (binary)
1 left shifted 4 is 10000 (binary)
  1. 1100110 (binary)
  2. + 10000 (binary)
  3. -------
  4. 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
*/
"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
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 38
Reputation: YoTaMiX is an unknown quantity at this point 
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Re: bitwise operations in C

 
0
  #3
May 1st, 2006
that code is actually adding 1 in place of index ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: bitwise operations in C

 
0
  #4
May 1st, 2006
"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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 482
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 47
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: bitwise operations in C

 
0
  #5
May 1st, 2006
  1. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC