Welcome, Unregistered.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Reply
Old 03-Aug-2012, 00:46   #26
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

Quote:
Originally Posted by cjo View Post
Nope, it is code written to be just about as un-obvious as possible. See if you can work out what it does :P
lol.... i'll give it a shot. but i just got to work so it will have to wait . would rather be learning and playing then designing internet gateways that have to conform to this pain in the arse http_://www.dsd.gov.au/infosec/ism/index.htm . thats the non classified edition the higher ones are burtal.
itsmydamnation is offline   Reply With Quote
Old 03-Aug-2012, 10:09   #27
Simon F
Tea maker
 
Join Date: Feb 2002
Location: In the Island of Sodor, where the steam trains lie
Posts: 4,379
Sony Playstation 3

Quote:
Originally Posted by itsmydamnation View Post
by when it comes to actually writing something i dont even like doing things like:

z = ( x >y ) ? x : y ;

i would rather

Code:
if ( x > y)
{
 z = x;
}
else
{
 z = y ; 
}
I think you just need to apply a rule of thumb - if you can see what a statement does at a glance (or at least without having to think about for more than a couple of seconds) then it's fine. Using that metric, I think the "z = ( x >y ) ? x : y ;" is perfectly acceptable, and it's certainly less verbose!.

Better, though, might be
Code:
#define MAX(X,Y)    ( (X)>(Y) ? (X)  : (Y)) 
....

    z = MAX(x,y);
Statements with multiple side effects, on the other hand, should be avoided. It will come back to bite you (or some other poor soul who has to maintain the code).
__________________
"Your work is both good and original. Unfortunately the part that is good is not original and the part that is original is not good." -(attributed to) Samuel Johnson

"I invented the term Object-Oriented, and I can tell you I did not have C++ in mind." Alan Kay
Simon F is offline   Reply With Quote
Old 03-Aug-2012, 11:56   #28
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

its more the operators in that instance I'm not used to, thus the need to think about it.

im about to try the ethatron C code juggernaut


edit: cant get it to compile

Code:
1>c:\projects\ethatron\ethatron\ethatron.c(14): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\projects\ethatron\ethatron\ethatron.c(19): warning C4244: '+=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(23): error C2065: 'Y' : undeclared identifier
1>c:\projects\ethatron\ethatron\ethatron.c(23): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(23): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(23): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(25): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(27): error C2065: 'Y' : undeclared identifier
1>c:\projects\ethatron\ethatron\ethatron.c(27): warning C4020: 'm' : too many actual parameters
1>c:\projects\ethatron\ethatron\ethatron.c(27): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(28): warning C4244: 'initializing' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(28): warning C4244: '=' : conversion from 'P' to 'N', possible loss of data
1>c:\projects\ethatron\ethatron\ethatron.c(29): error C2065: 'Y' : undeclared identifier
1>c:\projects\ethatron\ethatron\ethatron.c(29): warning C4020: 'm' : too many actual parameters

Last edited by itsmydamnation; 03-Aug-2012 at 12:07.
itsmydamnation is offline   Reply With Quote
Old 03-Aug-2012, 12:17   #29
entity279
Member
 
Join Date: May 2008
Location: Romania
Posts: 339
Send a message via Yahoo to entity279
Default

From my previous post: you're invoking the C++ compiler instead of the C one. You will also have to change that 'Y' @ ln27 to something else as that is plain wrong.
entity279 is offline   Reply With Quote
Old 03-Aug-2012, 12:23   #30
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

Quote:
Originally Posted by entity279 View Post
From my previous post: you're invoking the C++ compiler instead of the C one. You will also have to change that 'Y' @ ln27 to something else as that is plain wrong.
my quick google told me if i name the source file *.c it would then use the c compiler, i did that then got an error about incorrect headers ( c++ and not c) google and found the needed change for that. then i get those listed errors.


edit:

just tried this as well:
http://msdn.microsoft.com/en-us/library/bb384838.aspx
same issue


when it comes to programming im much like the old homer Simpson, ignorant not stupid

Last edited by itsmydamnation; 03-Aug-2012 at 12:33.
itsmydamnation is offline   Reply With Quote
Old 03-Aug-2012, 12:30   #31
Rodéric
a.k.a. Ingenu
 
Join Date: Feb 2002
Location: Apsley, U.K.
Posts: 2,727
Default

Quote:
Originally Posted by Simon F View Post
Better, though, might be
Code:
#define MAX(X,Y)    ( (X)>(Y) ? (X)  : (Y)) 
....

    z = MAX(x,y);
Statements with multiple side effects, on the other hand, should be avoided. It will come back to bite you (or some other poor soul who has to maintain the code).
The problem with a macro is that if x or y are, say, function calls, they are evaluated more than once, so a templated function is a better option. (You can force inline it if you need to.)
__________________
So many things to do, and yet so little time to spend...
Rodéric is offline   Reply With Quote
Old 03-Aug-2012, 12:45   #32
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

I initialized Y but now get

Code:
1>c:\projects\ethatron\ethatron\ethatron.c(36): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file
1>
edit: fixed with
#include"stdafx.h"
now compiles
itsmydamnation is offline   Reply With Quote
Old 03-Aug-2012, 12:45   #33
AlexV
Heteroscedasticitate
 
Join Date: Mar 2005
Posts: 2,354
Default

Quote:
Originally Posted by Simon F View Post
Better, though, might be
Code:
#define MAX(X,Y)    ( (X)>(Y) ? (X)  : (Y)) 
....
 
    z = MAX(x,y);
IMHO a macro is almost never better than anything if you have some other generic programming tool - in this case templates. Macros are pretty dubious in general, outside of uses like include guards or switching through debug/release builds, for example. But that's just me.
__________________
Donald Knuth: Science is what we understand well enough to explain to a computer. Art is everything else we do.
AlexV is offline   Reply With Quote
Old 03-Aug-2012, 12:46   #34
AlexV
Heteroscedasticitate
 
Join Date: Mar 2005
Posts: 2,354
Default

Quote:
Originally Posted by itsmydamnation View Post
I initialized Y but now get

Code:
1>c:\projects\ethatron\ethatron\ethatron.c(36): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file
1>
Disable precompiled header usage, StdAfx.h is VS's "nickname" for that.
__________________
Donald Knuth: Science is what we understand well enough to explain to a computer. Art is everything else we do.
AlexV is offline   Reply With Quote
Old 03-Aug-2012, 13:13   #35
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

well it now looks like this

Code:
// ethatron.cpp : Defines the entrAy point for the console application.
//
#include"stdafx.h"
#include<stdlib.h>
#include<stdio.h>


#define F for(
#define D return
#define E typedef
#define V malloc(sizeof
#define Z(x)(t(0,(x-C)/y)-l+1e-6*(x-Q))
#define i m(p(r,o,v),e,d 
E double P;E long N;E char*w;
N G,l,I,C,B,A,W,L,S,R,O,c,k,s=80,U=13,T=169;
P sqrt(P);
N H()
{
	F;
	O=scanf(" %1[#]%*[^\n]",&O);
	);
	scanf("%ld%*c",&O);
	D O;
}
P M(N R)
{
	s=s>=k?printf("%ld%c"+3*(C<1),s-k,R&7?32:10):s;
	D R>0?B/2?H():getchar():R?.9+.7*M(R+1):0;
}

P t(N x,P K)
{
	D x?(x>U?0:t(x+2,K)*(x-1)/x/K)+1/sqrt(K):t(2,K*K/U+1)*K;
}
N _(N J,N x)
{
	F*(x?&W:&A)=W+J+x;
	(O=x=W/(J=k*k))||A<J||((O=2*W/J)&&2*A/J<3);
	A+=A-J+1)
	{
		F J*=x+O;
		S--*B&&x==O;
		O=x=!J)
		{
			s+=x+s;
			M(0);
		}
		W+=W-J;
		S+=2;
		L+=L-J*k+M(S%8==B);
	}
	D 1;
}
P*p(P*J,P*x,P*O)
{
	F R=T;R--;)O[R]=J[R]+.8*x[R];D O;
}

P

m(P*W,P*x,P s)
{
	F R=U;
	--R;
	)
	{
		P*A=W+R*U;
		W[R]+=s/12;
		*W+=s;
		*A-=*x++;
		F;
		A>W;
		)
		{
			*A/=*W;
			F O=R;
			O;
			O--)A[O]-=W[O]**A;
			A-=U;
		}
	W+=14;
	}
	D*W;
}

int main(int z,w*y)
{
int Y;
N K=s,g;Y;
M(z);
s=B=C=15-H();
s=G=M(8);
s=l=M(C);
G*=g=C%3?1:3;

if(I=s=M(8))
	{
		N b=G+9,x=b*340+U,J=b*(l+5),*n=V(N)*J),*j=n+5*b;P*h=V(P)*x),*q=h+b,*e=q+b;
		P d=K;
		F;
		J--;
		)n[J]=J<b?0:I/2;
		F;
		x;
		)h[--x]=0;
		s=M(x);
		k=256;
		C=4-C;
		B=C/4;
		s=_(_(T,0),0);
		F;--z;
		)
		
		{
			R=atoi(*++y);
			c=R>0?K=R,c:-2*R;
		}
	
		z=G/K+1;
		z+=g>z;
		I++;
		c++;
		F;
		++J<l;
		)
		{
			P u=0,C,y;
			P*H=e+U,*o=H+b*T,*r=o-2*T,*v=r-T;
			F;
			x<G;
			x++)
			{
				p(H,o-T,o);
				o+=T;
				H+=T;
				q[x]=.7*(q[x-1]+h[x]);
			}
		
			p(H,H,r);
			F;
			x;
			)
			{
				P*S=e;
				w l=" !{   ,;lf6D@";j+=1-g;F;
				*++l;
				)
				{
					*S++=*j;
					j+=*l%3*g-g+*l%5*b-3*b;
				}
				y=M(x-G);
				y=M(-J)*M(-x)+M(-J-1)*y+.01;
				y=sqrt((u+q[--x]+.1)/y);
				o-=T;C=i)+.5;K=M(-B);
				{
					N f=I,Q=0;F;
					f-Q>c;
					)
					{
						P l=0;
						N H=C+c/2+1;
						H%=c;
						H+=(f+Q+c-2*H)/2/c*c;
						l=Z(Q);
						O=(A-W)*Z(H)/Z(f);
						_(O,R=B?K>=H:L/k>O+W);
						*(R?&Q:&f)=H;
					}
				
					Q+=f;
					*S=*j=Q/=2;
					s=B?s:Q+k;
					f=Q+n[x/z];
					n[x/z]=x%z+J%(z*2/g)?f:!putc(x?l[4*g*f/z/z/I-8]:10,stderr);
					H-=U;F O=156;O--;
					)
						{
						H--;
						*H=e[O%U]*e[O/U]/y+.8**H;
						}

						C-=Q+.5;y=i/.9)-Q-C;p(p(r+T,r,r),H,r);
						h[x]=.7*h[x]+C*C;
						u+=h[x];
						u*=.7;
						d+=C>0?-y:y;

					}
				}
			j+=9;
			}
		_(_(x,x),x);
		}
	D 0;
}
still zero idea what is going on ......lol
itsmydamnation is offline   Reply With Quote
Old 03-Aug-2012, 14:18   #36
cjo
Member
 
Join Date: Mar 2010
Posts: 132
Default

Quote:
Originally Posted by itsmydamnation View Post
well it now looks like this
<snip>

still zero idea what is going on ......lol
Try running it through the preprocessor (I'll let you use google to work out how)
cjo is offline   Reply With Quote
Old 03-Aug-2012, 16:24   #37
Simon F
Tea maker
 
Join Date: Feb 2002
Location: In the Island of Sodor, where the steam trains lie
Posts: 4,379
Default

Quote:
Originally Posted by Rodéric View Post
The problem with a macro is that if x or y are, say, function calls, they are evaluated more than once, so a templated function is a better option. (You can force inline it if you need to.)
/me looks in his "ANSI C. A Lexical Guide".... no mention of template in the index.
__________________
"Your work is both good and original. Unfortunately the part that is good is not original and the part that is original is not good." -(attributed to) Samuel Johnson

"I invented the term Object-Oriented, and I can tell you I did not have C++ in mind." Alan Kay
Simon F is offline   Reply With Quote
Old 03-Aug-2012, 22:36   #38
Dominik D
Member
 
Join Date: Mar 2007
Location: Wroclaw, Poland
Posts: 578
Default

As for the OP - undefined behavior means exactly that: undefined behavior. Both x and y can end up with any value compiler thinks is correct (or randomizes by looking at the weather in Kuala Lumpur). On top of that compiler can do whatever it wants extra, e.g. execute printf("This code sucks");. Since this is undefined behavior it would have been perfectly valid to do it and compilers have done this in the past - GCC 1.17 would launch NetHack or Rogue upon discovering undefined behavior in the code. Trying to reason "this should be X" or "why do I get Y" is absolutely pointless.
__________________
Shifty Geezer: I don't think the guy really understands the subject.
PARANOiA: To be honest, Shifty, what you've described is 95% of Beyond3D - armchair experts spouting fact based on the low-level knowledge of a few.

This posting is provided "AS IS" with no warranties, and confers no rights.
Dominik D is offline   Reply With Quote
Old 04-Aug-2012, 08:43   #39
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

had a go at the start of grey-scale image compressor ( did you write this code Ethatron?). Pretty hard with kids about and the logic of some of the macro's is beyond me

Code:
// replace F with for(
#define F for(
// replace D with return
#define D return
//replace E with typedef
#define E typedef

// replace V with malloc(sizeof
// malloc is used to request/allocate a block of memory sized in bytes, block isn't initalized
#define V malloc(sizeof
// a macro that i cant figure out how to read it
#define Z(x)(t(0,(x-C)/y)-l+1e-6*(x-Q))
// another marco of stuff..lol
#define i m(p(r,o,v),e,d
//use typedef to create aliases fp double = P, long in = N , 1byte char = *w
E double P;
E long N;
E char*w;
//all of these are long ints
N G,l,I,C,B,A,W,L,S,R,O,c,k,s=80,U=13,T=169;
// returns square root, cant see the point in it yet still builds when i comment it out :)
P sqrt(P);

//fuction called H
N H()
{
	// FOR (
	F;
	// takes input data dont understand the paramaters 
	// & is bitwise AND , seems to care about the data last 
	// time fuction was ran, output is O and O is used within the function
	O=scanf(" %1[#]%*[^\n]",&O);
	
	);

	scanf("%ld%*c",&O);
	//returns O
	D O;
itsmydamnation is offline   Reply With Quote
Old 04-Aug-2012, 12:22   #40
cjo
Member
 
Join Date: Mar 2010
Posts: 132
Default

Quote:
Originally Posted by itsmydamnation View Post
had a go at the start of grey-scale image compressor ( did you write this code Ethatron?). Pretty hard with kids about and the logic of some of the macro's is beyond me
If you run it through the preprocessor it will replace all the #defines for you.
cjo is offline   Reply With Quote
Old 04-Aug-2012, 14:51   #41
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,489
Default

I was under the impression C++ was an extension of C and that a C++ compiler could compile programs written in C
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 04-Aug-2012, 17:18   #42
Rodéric
a.k.a. Ingenu
 
Join Date: Feb 2002
Location: Apsley, U.K.
Posts: 2,727
Default

Your impression is mostly correct.
C++ compilers have historically just been patched up C compilers, which explains why many C++ features took ages to get integrated (and spec compliant.).
__________________
So many things to do, and yet so little time to spend...
Rodéric is offline   Reply With Quote
Old 04-Aug-2012, 19:05   #43
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,489
Default

Mostly, but the op couldnt compile the C code with his C++ compiler and had to use a C compiler
And if I was correct and C++ compilers did compile C why would C compilers (made post C++ introduction) exist
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 05-Aug-2012, 05:12   #44
Ethatron
Member
 
Join Date: Jan 2010
Posts: 375
Default

Quote:
Originally Posted by itsmydamnation View Post
had a go at the start of grey-scale image compressor ( did you write this code Ethatron?).
Hey, you found out what it is. (thumb up)
No, it's from Bernie Meyer. He was participating in a smallest code contest, that's why there is the macro code-implosion in it. He was also playing with image compression at the time, which he took to try himself in the contest. For quite some time this was the most efficient lossless image compressor available.
To me it's the most perfect source I've ever seen, it's funny (in various ways), it's artistic, it's efficient, it's marvelous (you ran it once? a possible commandline is like this: "program <input.pgm >output.coded"), it's also playful ironic (sarkastic) in regards to I-have-the-new-better-algorithm science. Like the eggs from Kinderüberraschung.
The variables give away its name if you want to search for it.

"P sqrt(P)" is a protoype BTW, no need to include math.h
"& is bitwise AND" is wrong, & is the get-reference-of-variable operator. In C that's about it (and reference means pointer), in C++ you can also declare variables to be references instead of containers (and they are distinct types). I think actually that would be a good start for learning a pure C++ syntax/concept. And if you learn that first you may not get the bad habit of using pointers all the time, it nicely goes with operator overloading as well, which I would recommend next.

All the other stuff you've shown is the same in C (?: is called ternary operator, ++ increment and -- decrement also are part of C), and a lot of other languages.
Ethatron is offline   Reply With Quote
Old 05-Aug-2012, 13:07   #45
pcchen
Moderator
 
Join Date: Feb 2002
Location: Taiwan
Posts: 2,347
Default

Quote:
Originally Posted by Davros View Post
Mostly, but the op couldnt compile the C code with his C++ compiler and had to use a C compiler
And if I was correct and C++ compilers did compile C why would C compilers (made post C++ introduction) exist
Although most C programs are valid C++ programs, not all C programs are valid C++ programs. The most obvious examples are those with variable names that happens to be C++ keywords (that also influences the choice of C++ keyword names).
pcchen is offline   Reply With Quote
Old 05-Aug-2012, 13:43   #46
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

Quote:
Originally Posted by Ethatron View Post
Hey, you found out what it is. (thumb up)
No, it's from Bernie Meyer. He was participating in a smallest code contest, that's why there is the macro code-implosion in it. He was also playing with image compression at the time, which he took to try himself in the contest. For quite some time this was the most efficient lossless image compressor available.
To me it's the most perfect source I've ever seen, it's funny (in various ways), it's artistic, it's efficient, it's marvelous (you ran it once? a possible commandline is like this: "program <input.pgm >output.coded"), it's also playful ironic (sarkastic) in regards to I-have-the-new-better-algorithm science. Like the eggs from Kinderüberraschung.
The variables give away its name if you want to search for it.

"P sqrt(P)" is a protoype BTW, no need to include math.h
"& is bitwise AND" is wrong, & is the get-reference-of-variable operator. In C that's about it (and reference means pointer), in C++ you can also declare variables to be references instead of containers (and they are distinct types). I think actually that would be a good start for learning a pure C++ syntax/concept. And if you learn that first you may not get the bad habit of using pointers all the time, it nicely goes with operator overloading as well, which I would recommend next.

All the other stuff you've shown is the same in C (?: is called ternary operator, ++ increment and -- decrement also are part of C), and a lot of other languages.
well im trying a pointer at the moment and getting a little confused.

what im actually trying to do is print decimal values as binary using _itoa_s in stdlib.h.
I have it working but i dont understand why it is working

if i have code like this

Code:
void bit_shift()
{
	using namespace std;

	clearscreen();
	int dec_number = 0;
	cout << " showing how bit shift works " << endl ;
	cout << "please enter an integer number  :";
	cin >> dec_number ;
	
	char *binary_value=0;

	//returns ptr to array
	binary_value = binary_conversion (dec_number);
	
	
	std::cout << &binary_value << endl;

}

char *binary_conversion(int i)
{
		//char *buffer_return = new char[33];
		static char buffer[33];
		_itoa_s (i,buffer,2);
	
	//std::cout << buffer_return ;
	return buffer;

}
std::cout << &binary_value << endl; returns a memory address which im fine with

but if i change that to std::cout << &binary_value[0] << endl; i get the contents of the entire array (the desired result) but i dont understand why .

if i change it to std::cout << *binary_value << endl; i just get the first element of the array ( unless 0 it will always be 1)

i tried something like
Code:
for ( int i =0 ; i < 34 ; i++)
{
std::cout << *binary_value[i] << endl
}
but it kept throwing compile errors and i couldn't get it to work. which from my limited understanding makes sense as the pointer should be just the address of the first element.
itsmydamnation is offline   Reply With Quote
Old 05-Aug-2012, 13:56   #47
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,489
Default

try

#include <iostream>
#include <string>
#include <cmath>

int main()
{
std::string binary;
double decimal = 0;

std::cout << "Enter the binary value you wish to convert into decimal: " << endl;
std::cin >> binary;

for(int counter = 0; counter < binary.size(); counter++)
if(binary.c_str()[counter] == '1')
decimal += pow(2, counter);

cout << "The decimal representation of the given binary is: " << decimal << endl;
return 0;
}

edit: may be missing
std::endl
std::cout
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 05-Aug-2012, 14:06   #48
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

cheers

but my code works fine:

output:
Quote:
showing how bit shift works
please enter an integer value :213
the number you entered 213 in binary is :11010101
with 1 bit shifted left 426 in binary is :110101010
with 2 bit shifted left 852 in binary is :1101010100
with 1 bit shifted right 106 in binary is :1101010
with 2 bit shifted right 53 in binary is :110101

its more i don't understand why &binary_value[0] returns the entire array when &binary_value returns a memory address and *binary_value returns a single element of the array
itsmydamnation is offline   Reply With Quote
Old 05-Aug-2012, 15:31   #49
pcchen
Moderator
 
Join Date: Feb 2002
Location: Taiwan
Posts: 2,347
Default

Quote:
Originally Posted by itsmydamnation View Post
its more i don't understand why &binary_value[0] returns the entire array when &binary_value returns a memory address and *binary_value returns a single element of the array
That's because &binary_value[0] is a char* type, which is treated as a string in C. On the other hand, *binary_value is a char type, so it's a simple value.
pcchen is offline   Reply With Quote
Old 07-Aug-2012, 15:55   #50
itsmydamnation
Member
 
Join Date: Apr 2007
Location: Australia
Posts: 645
Default

Well i spend the last two nights before bed working pretty much on this...

At first i thought it would be cool. Now i am never doing anything as stupid pointless and insane again ;

heres the code
Code:
void OR_XOR()
{
	using namespace std;
	int dec_number_1 = 0;
	cout << " showing how XOR, OR and AND works " << endl ;
	cout << "please enter an integer number  :";
	cin >> dec_number_1 ;
	
	int dec_number_2 = 0;
	cout << "please enter a second integer number  :" ;
	cin >> dec_number_2 ;
	cout <<  endl << endl <<endl ;

	//libary fuction to convert dec number into binary for first number
		char buffer_1[33];
	_itoa_s (dec_number_1,buffer_1,2);
	
	//counts the number of binary digits with the array, 
	//aka elements with either 1 or 0 in them
	int count_1= 0;
	for each (char i in buffer_1)
	{
		
		//std::cout << i ;
		if (i ==48 || i ==49) {
			count_1++ ; 
			
		}
	}

	
	//libary fuction to convert dec number into binary for second number
	 char buffer_2[33];
	_itoa_s (dec_number_2,buffer_2,2);
	
	//counts the number of binary digits with the array, 
	//aka elements with either 1 or 0 in them
	int count_2=0;
	 for each (char j in buffer_2)
	 {
		 if (j ==48 || j==49)
		 {
			 count_2++;
		 }

	 }

	// used to figure out which "number" is longer in binary length
	// positive number = first is longer, neg number second is longer
	int bit_length_difference=0;
	bit_length_difference = count_1 - count_2;
	int XOR_count =0;
	int OR_count =0;
	int AND_count=0;
	if ( count_1 > count_2)
	{
		XOR_count =count_1;
		OR_count =count_1;
		AND_count=count_1;
	}
	else
	{
		XOR_count =count_2;
		OR_count =count_2;
		AND_count=count_2;
	}

	// final array for first number converted from char to int and will be 
	// same binary lenth as second_number_final
	int first_number_final[33];
	int second_number_final[33];
	int XOR_array[33];
	int XOR_DEC_value=0;
	int OR_array[33];
	int OR_DEC_value=0;
	int AND_array[33];
	int AND_DEC_value=0;
	//used as a counter for first_number_final an second_number_final array
	int count_fnf=0;
	int count_snf=0;
	
	//if b_l_d is less then 0 then second number is longer in binary digits then first
	// create a new array and populate its lowest elements with int 0 until addition 0's plus buffer_1 binary length is 
	// the same as buffer_2.
	if ( bit_length_difference < 0)
	{
		
		//adds the value 0 to elements in first_number_final until
		// bit_length_difference = 0;
		while ( bit_length_difference != 0)
		{
			first_number_final[count_fnf] = 0;
			bit_length_difference++;
			count_fnf++;
		}
		//used in whileloop to track elements in buffer_1
		int i = 0;
		while ( count_1 != 0)
		{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_1[i] == 48)
			{
			first_number_final[count_fnf]=0;
			i++;
			count_fnf++;
			count_1--;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_1[i] == 49)
			{
			first_number_final[count_fnf]=1;
			i++;
			count_fnf++;
			count_1--;
			}
		}
		
		//used in whileloop to track elements in buffer_2
		int k = 0;
		//used to convert the second number to binary
		while ( count_2 != 0)
			{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_2[k] == 48)
			{
				second_number_final[count_snf]=0;
				k++;
				count_snf++;
				count_2--;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_2[k] == 49)
			{
				second_number_final[count_snf]=1;
				k++;
				count_snf++;
				count_2--;
			}

		}
	}else if (bit_length_difference > 0) 
	{
		//adds the value 0 to elements in second_number_final until
		// bit_length_difference = 0;
		while ( bit_length_difference != 0)
		{
			second_number_final[count_snf] = 0;
			bit_length_difference--;
			count_snf++;
		}
		//used in whileloop to track elements in buffer_1
		int i = 0;
		while ( count_2 != 0)
		{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_2[i] == 48)
			{
				second_number_final[count_snf]=0;
				i++;
				count_snf++;
				count_2--;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_2[i] == 49)
			{
				second_number_final[count_snf]=1;
				i++;
				count_snf++;
				count_2--;
			}
		}

		//used in whileloop to track elements in buffer_1
		int k = 0;
		//used to convert the second number to binary
		while ( count_1 != 0)
			{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_1[k] == 48)
				{
				first_number_final[count_fnf]=0;
				k++;
				count_fnf++;
				count_1--;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_1[k] == 49)
				{
				first_number_final[count_fnf]=1;
				k++;
				count_fnf++;
				count_1--;
			}

		}
	//both values have same binary lenght
	}
	else
	{
		//used in whileloop to track elements in buffer_1 and buffer_2 as they are the same length
		int k = 0;
		
		//used to convert the second number to binary
		while ( count_1 != 0)
		{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_1[k] == 48)
			{
				first_number_final[count_fnf]=0;
				count_fnf++;
				count_1--;
				std::cout << count_fnf;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_1[k] == 49)
			{
				first_number_final[count_fnf]=1;
				count_fnf++;
				count_1--;
				std::cout << count_fnf;
			}
			if (buffer_2[k] == 48)
			{
				second_number_final[count_snf]=0;
				count_snf++;
				std::cout << count_snf << std::endl;
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_2[k] == 49)
			{
				second_number_final[count_snf]=1;
				count_snf++;
				std::cout << count_snf << std::endl;
				
			}
			k++;
			
		}

	}
	std::cout << " the first number you entered in binary is : \t \t " ;
	int i=0;

	while (i != count_fnf)
	{
		std::cout << first_number_final[i] ;
		i++;
	}

	std::cout << std::endl;
	std::cout << " the second number you entered in binary is : \t \t " ;
	int k=0;
	while (k != count_snf)
	{
		std::cout << second_number_final[k] ;
		k++;
	}
	std::cout << std::endl;
	
	int j =0;
	int power = XOR_count -1;
	int power_temp = 0;
	float power_of_2 = 2;
	while (j != XOR_count)
	{
		
		XOR_array[j] = first_number_final[j] ^ second_number_final[j];
		
		if ( XOR_array[j] == 1)
		{
			if ( j == (count_fnf-1))
			{
				XOR_DEC_value = XOR_DEC_value + 1;
			}
			else
			{
			
			double power_temp =pow(power_of_2,power);
			XOR_DEC_value = XOR_DEC_value + power_temp ;
			
			
			}
		
		}
		power--;
		j++;
	}
	std::cout << "XOR'd number based off your numbers in binary is :  \t " ;
	int l=0;
	while (l != XOR_count)
	{
		std::cout << XOR_array[l] ;
		l++;
	}
	std::cout << std::endl;
	std::cout << "the dec value of the XOR number is : \t \t \t " << XOR_DEC_value << std::endl;


	int M =0;
	int power_OR = OR_count -1;
	int power_OR_temp = 0;
	while (M != OR_count)
	{
		
		OR_array[M] = first_number_final[M] | second_number_final[M];
		
		if ( OR_array[M] == 1)
		{
			if ( M == (count_fnf-1))
			{
				OR_DEC_value = OR_DEC_value + 1;
			}
			else
			{
			
			double power_OR_temp =pow(power_of_2,power_OR);
			OR_DEC_value = OR_DEC_value + power_OR_temp ;
			
			
			}
		
		}
		power_OR--;
		M++;
	}
	std::cout << "OR'd number based off your numbers in binary is :  \t " ;
	int N=0;
	while (N != XOR_count)
	{
		std::cout << OR_array[N] ;
		N++;
	}
	std::cout << std::endl;
	std::cout << "the dec value of the OR number is : \t \t \t " << OR_DEC_value << std::endl;

	int O =0;
	int power_AND = AND_count -1;
	int power_AND_temp = 0;
	while (O != OR_count)
	{
		
		AND_array[O] = first_number_final[O] & second_number_final[O];
		
		if ( AND_array[O] == 1)
		{
			if ( O == (count_fnf-1))
			{
				AND_DEC_value = AND_DEC_value + 1;
			}
			else
			{
			
			double power_AND_temp =pow(power_of_2,power_AND);
			AND_DEC_value = AND_DEC_value + power_AND_temp ;
			
			
			}
		
		}
		power_AND--;
		O++;
	}
	std::cout << "AND'd number based off your numbers in binary is :  \t " ;
	int P=0;
	while (P != AND_count)
	{
		std::cout << AND_array[P] ;
		P++;
	}
	std::cout << std::endl;
	std::cout << "the dec value of the AND number is : \t \t \t " << AND_DEC_value << std::endl;
	std::cout << std::endl << std::endl << std::endl << std::endl << std::endl;
}
and here is the output

Code:
 showing how XOR, OR and AND works
please enter an integer number  :1243
please enter a second integer number  :123



 the first number you entered in binary is :             10011011011
 the second number you entered in binary is :            00001111011
XOR'd number based off your numbers in binary is :       10010100000
the dec value of the XOR number is :                     1184
OR'd number based off your numbers in binary is :        10011111011
the dec value of the OR number is :                      1275
AND'd number based off your numbers in binary is :       00001011011
the dec value of the AND number is :                     91
Why is it so stupidly big, because all the calculations within the code are binary, it will work for upto 32 bit numbers. I could have done a lot of code reuse but because of the issues above i couldn't do that,

Now its 1:00am and time for bed..... work in 7 hours

Last edited by itsmydamnation; 07-Aug-2012 at 16:04.
itsmydamnation is offline   Reply With Quote

Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:15.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.