473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Binary Arithmetics

Hello I am trying to add a long to long and I need some help
example:
01101011
00001000
--------
????????
does anyone have any idea how to make a simple function(a,b)

I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.
thanks

Nov 14 '05 #1
13 1842
<ke****@gmail.com> wrote:
Hello I am trying to add a long to long and I need some help
example:
01101011
00001000
--------
????????
does anyone have any idea how to make a simple function(a,b)

I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.


Take a look at this link.

http://en.wikipedia.org/wiki/Binary_arithmetic#Addition
Nov 14 '05 #2
that is cool but I know that part I don't know how I can make write
that in C
thanks anyway

Nov 14 '05 #3
In article <11**********************@g14g2000cwa.googlegroups .com>,
ke****@gmail.com <ke****@gmail.com> wrote:
I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.
Take a look at this link. http://en.wikipedia.org/wiki/Binary_arithmetic#Addition that is cool but I know that part I don't know how I can make write
that in C


Please take -another- look at the link, as the algorithm you described
(which I quoted above) is not correct.
--
This signature intentionally left... Oh, darn!
Nov 14 '05 #4
ke****@gmail.com wrote:
Hello I am trying to add a long to long and I need some help
example:
01101011
00001000
--------
????????
does anyone have any idea how to make a simple function(a,b)
inline long longadd(long a, long b) { return a+b; }
I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.


Who cares?
Nov 14 '05 #5
ke****@gmail.com wrote:
Hello I am trying to add a long to long and I need some help
example:
01101011
00001000
--------
????????
does anyone have any idea how to make a simple function(a,b)

I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.
thanks


I am not really sure what your intented goal is, but realize this:

result = first XOR second
carry = first AND second

in code that would be:

r = b1 ^ b2;
c = b1 & b2;

Of course you have to incorporate the carry in the following steps as well
(so really you are adding three numbers). But this is trivial stuff for any
CPU, so again am not sure why you'd want C code to accomplish this.

Good luck,

--
Martijn
http://www.sereneconcepts.nl
Nov 14 '05 #6
If you post the entire homework question, it'll be a lot more clear.

--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Nov 14 '05 #7
In article <sj********************************@4ax.com>,
KQ****@IEEInc.com says...
If you post the entire homework question, it'll be a lot more clear.


:-)

Nov 14 '05 #8
thanks I figure it out, wasn't a homework sorry ;-)
I was trying to add numbers to time_t types and to change the time
without converting to tm structure.

Nov 14 '05 #9
ke****@gmail.com wrote:
Hello I am trying to add a long to long and I need some help
example:
01101011
00001000
--------
????????
does anyone have any idea how to make a simple function(a,b)

I figure out that if I AND a to be is working but is 1 is ANDing to 1
the next left bit needs to be set as 1.
thanks

Hi,

i would define 2 functions, one fo a half adder, and one for a full adder.

With this functions you can make a n-Bit adder (using the full adder
function recursively)


Definitions:
&& ... logical AND (^)
&& ... logical OR (v)
! ... logical NOT (_)
truth table of Half adder:

A B C Z
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

Equations:
Z=(A! && B) || (A && B!)
C=(A && B)

truth table of Full adder:

C B A Cr Z
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1

Equations:
Z=(A && B! && C!) || (A! && B && C!) || (A! && B! && C) || (A && B && C)
Cr= (A && B && C!) || (A && B! && C) || (A! && B && C) || (A && B && C)

Karnaughoptimization of Carry:
Cr=(A && B) || (B && C) || (A && C)
Merging HA and FA to (as example) 2-Bit Adder:

Input: 2 2-bit numbers
A1,A0
B1,B0

A0, B0 -> HA -> Z0,C0
A1, B1, Cr1=C0 -> Z1, Carry

Result: Z1,Z0, Carry

If you want to add a 3 Bit number, you just have to extend it using
another Full adder for A2,B2,C2=Carry ... and so on

Nov 14 '05 #10
That's going to be completely non-portable, because there's no guarantee what bits in a time_t
mean.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Nov 14 '05 #11
Kevin D. Quitt <KQ****@IEEInc.com> wrote:
That's going to be completely non-portable,


_What_ is? You haven't even the excuse of using Google Broken Beta, so
_quote_ something, for goodness' sake!
(Hint: Options -> Posting Prefs -> General -> Include original text.)

Richard
Nov 14 '05 #12
"ke****@gmail.com" <ke****@gmail.com> writes:
thanks I figure it out, wasn't a homework sorry ;-)
I was trying to add numbers to time_t types and to change the time
without converting to tm structure.


Huh?

The standard only guarantees that time_t is an arithmetic type capable
of representing times.

If you're willing to assume that it represents times monotonically in
seconds, then adding, say, 60 seconds is trivial: t + 60. This
assumption will be valid for many systems, but of course it's strictly
non-portable.

If you're not willing to make this assumption, you pretty much need to
use "struct tm" if you want to manipulate times.

In neither case, as far as I can tell, does it make any sense to
emulate ordinary arithmetic by manually twiddling bits. If you think
it does, then either you're mistaken or I'm missing something about
the problem you're trying to solve.

You'll often get more useful help if you'll tell us *why* you're
trying to do something.

<METAPHOR>
If you ask us for a screwdriver with a really strong handle, we can
probably tell you how to get one. If you ask us for a screwdriver
with a really strong handle so you can drive nails with it, we can
tell you to put down the screwdriver and get a hammer.
</METAPHOR>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13
On Fri, 29 Apr 2005 06:42:47 GMT, rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
_quote_ something, for goodness' sake!


I'm so sick and tired of people quoting entire articles for a "me, too", that I guess I went a
little far in the other direction. FWIW: he wanted to do arithmetic on a time_t.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
20
by: Christian Stigen Larsen | last post by:
A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative...
6
by: Terence | last post by:
I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; char *ptr = s; ptr += 1; // I assume ptr is increased by 1 byte, pointing to the 2nd element in the...
21
by: Milan Čermák | last post by:
Hi all, I'm about to write an arithmetics object library. It should contain classes like ShortInteger (for 32-bit longs), Double and Float as standard numeric type wrappers and LongInteger (really...
1
by: Jonas Ernst | last post by:
Hi, Can somebody give me some hints how to do a line interpolation without using floating point arithemtics? The function shall do a linear interpolation between 2 points (line interp?) and...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
8
by: Dreadknought | last post by:
I'm new to Python I'm trying to create a program that converts decimal to binary but am having trouble. I understand the arithmetics of it, but I was wondering how I can add the remainders onto...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
2
by: Draggonn | last post by:
Hello all! I'm having a little problem with binary numbers. The teacher didn't quite explain them to us (saying we won't need them during the course I'm currently taking). But I know they are very...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.