had a go at the start of grey-scale image compressor ( did you write this code Ethatron?).
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
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. :smile:
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.
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;
}
for ( int i =0 ; i < 34 ; i++)
{
std::cout << *binary_value[i] << endl
}
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
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;
}
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
You have to think a bit different in C++ than in perl and you're also much nearer the hardware, but you'll eventually write shorter code than that if you continue exercising.
but you'll eventually write shorter code than that if you continue exercising.
But i haven't really moved past C yet.
Davros, I appreciate lulz as much as the next guy, but beyond a certain threshold just lulzing for the lulz of lulz turns into noise. Moreover, the premise of the thread is serious, whereas "Teach yourself XYZ in 24 hours" is not.
I've seen basically the whole world praise Accelerated C++ as a book. But i thought it was more an intermediate level knowledge kind of book? Do you think its worth jumping straight into it? I have also heard quite a few people praise this book Beginning C++ Through Game Programming which a lot of people like because all the examples are game orientated.