RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 2624 | Replies: 4
Reply
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation: YoTaMiX is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Help bitwise operations in C

  #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, 4 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,757
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: bitwise operations in C

  #2  
May 1st, 2006
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
*/
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."
Reply With Quote  
Join Date: Jan 2006
Location: Israel
Posts: 37
Reputation: YoTaMiX is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
YoTaMiX YoTaMiX is offline Offline
Light Poster

Re: bitwise operations in C

  #3  
May 1st, 2006
that code is actually adding 1 in place of index ?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,757
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: bitwise operations in C

  #4  
May 1st, 2006
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."
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: bitwise operations in C

  #5  
May 1st, 2006
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:41 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC