GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Apr-2005, 14:19
lontra lontra is offline
New Member
 
Join Date: Apr 2005
Posts: 3
lontra is on a distinguished road

How to do logical and arithmetic operations?


I am trying to do logical and mathematical operations in C in a bit level. I want to do sum, subtraction, AND, XOR and OR operations, but the only information that I got about this subject is the code that in the txt file, and I quite don't understand how the last function (void showbits) is converting the number from the hexadecimal to the binary form.

So if someone can tell me how the last function is doing the process of converting the typed number and give me a very good idea about how I can do logical and mathematical operations in the binary level I would be very pleased. Thanks in advance
Attached Files
File Type: txt bits.txt (489 Bytes, 30 views)
  #2  
Old 26-Apr-2005, 14:36
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
I do not want to look as if I am self promoting my self or site, but I've written a little tutorial over bitwise operators (yes Walt and dsmith, I finally figured them out ) which you can find here, if that helps at all. It also includes code to add and subtract using only bitwise operators.
  #3  
Old 26-Apr-2005, 16:23
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by lontra
I am trying to do logical and mathematical operations in C in a bit level. I want to do sum, subtraction, AND, XOR and OR operations, but the only information that I got about this subject is the code that in the txt file, and I quite don't understand how the last function (void showbits) is converting the number from the hexadecimal to the binary form.

So if someone can tell me how the last function is doing the process of converting the typed number and give me a very good idea about how I can do logical and mathematical operations in the binary level I would be very pleased. Thanks in advance

My new mantra: Read the Sticky
It's for all who post questions...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #4  
Old 03-May-2005, 02:14
lontra lontra is offline
New Member
 
Join Date: Apr 2005
Posts: 3
lontra is on a distinguished road
Quote:
Originally Posted by Dr. Evil
I do not want to look as if I am self promoting my self or site, but I've written a little tutorial over bitwise operators (yes Walt and dsmith, I finally figured them out ) which you can find www.cjb.cc, if that helps at all. It also includes code to add and subtract using only bitwise operators.

Thanks, the part of shift helped to understand a little more, but I still have a lot of doubts regarding other parts. I made a modified version of your program that looks like this:

CPP / C++ / C Code:
#include <stdio.h>

int ALU ( int n1, int n2);

main ()
{
   int  n1, n2, i;

   while (5 == 5) {
       printf ("\nTo close the program type 0: ");
       scanf  ("%d", &i);
       if     (i == 0) break;
       printf ("\nType the two numbers for the operation: ");
       scanf  ("%d %d", &n1, &n2);
       ULA    (n1, n2); 
   }
}

int ULA ( int n1, int n2)
{
    int bit, carry, tmp;
    carry = n1 & n2; //Get the carry bits
    bit = n1 ^ n2; //Get the answer bits
    while(carry) //Loop until we have rid ourselves of the carry bits
    {
	carry <<= 1; //shift carry bits one so they really will 'carry'
	tmp = bit ^ carry; //Exclusive OR the carry and answer to get the next answer
        carry &= bit; //Get rid of any carry bits that were used and gain any new ones
        bit = tmp;
	}
	printf ("%d", bit);
	scanf  ("%d", &bit);
	return bit;
}

I am not sure if I really understood the concept of the carry bit and how I can show the result of the sum in binary?

P.S: And how I can put my code inside one of these special code regions?
Last edited by LuciWiz : 04-May-2005 at 00:12. Reason: Please insert your C code between [c] & [/c] tags
  #5  
Old 03-May-2005, 07:56
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
Quote:
I am not sure if I really understood the concept of the carry bit
The 'carry' variable just contains all the bits that need to be carried over to be added in on the next bit over. So if you had 0101 (5) and 0110 (6), then the carry variable would be 0100 (using the AND operator) and the 'bit' variable, using XOR, would be 0011 after the first iteration. The carry bit then needs to be shifted over one before it is XORed into the 'bit' variable (again), to get a final answer of 1011 (11).

Quote:
how I can show the result of the sum in binary?
If you want to show the bits of a number, the easiest way is probably to just cycle through all the bits and print 0 or 1 accordingly:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

void showbits(unsigned long num)
{
	unsigned long i;
	
	for(i = 1 << ((sizeof(num) << 3) - 1); i; i >>= 1)
		putchar(num & i ? '1' : '0');
	
	return;
}

int main(int argc, char *argv[])
{
	if(argc == 2) showbits(atol(argv[1]));
	
	return 0;
}

And finally, you can use [ c][/c ] tags (without spaces) to format your code all purdy like.
  #6  
Old 03-May-2005, 22:55
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I repeat:

Read the Sticky
It's for all who post questions... including lontra
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 10-May-2005, 00:20
lontra lontra is offline
New Member
 
Join Date: Apr 2005
Posts: 3
lontra is on a distinguished road
Thanks Dr. Evil, no more doubts.

WalterP, after had read your first message I gave a quick read in the sticky and didn't perceive anything wrong with my messages. I assume that you was talking about the lack of formatting of the code, right ? If this is the case, at the time I didn't know that this option existed, and soon after had read the second message of Dr Evil I realized it and asked him how to use it.
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.