Skip to main content

Hello World!

1 like • Aug 31, 2020 • 2 views
C++
Loading...

More C++ Posts

Bit arithmetic + and -

0 likes • Sep 1, 2023 • 2 views
C++
#define NUM_BITS 8
#include <iostream>
struct Number{
int num : NUM_BITS;
Number(){}
Number(const int& bruh){
num = bruh;
}
operator int() const { return num; }
Number& operator=(const int& bruh){
num = bruh;
return (*this);
}
};
using namespace std;
bool isNegative(const int& num){
//This gets the bitwise and of num and 10000000000000000000000000000000
//This implicit casts to bool, which means (num & (1 << 31)) != 0
return (num & (1 << 31));
}
void printBinaryNumber(const int& num, const int numBits){
for(int i = numBits; i > 0; --i){
//8..1
int bitMask = 1 << (i-1);
if(num & bitMask){ //Test the bit
cout << '1';
}else{
cout << '0';
}
}
}
void printCarryBits(const int& a, const int& b, const int numBits){
int answer = 0;
bool carry = false;
for(int i = 0; i < numBits; ++i){
//8..1
int bitMask = 1 << i;
bool aBit = a & bitMask;
bool bBit = b & bitMask;
if(aBit && bBit || aBit && carry || bBit && carry){ //Carry bit is true next
if(carry)
answer |= bitMask;
carry = true;
}else{
if(carry)
answer |= bitMask;
carry = false;
}
}
printBinaryNumber(answer, 8);
}
void printBorrowBits(const int& a, const int& b, const int numBits){
int answer = 0;
bool carry = false;
for(int i = 0; i < numBits; ++i){
//8..1
int bitMask = 1 << i;
bool aBit = a & bitMask;
bool bBit = b & bitMask;
if((!(aBit ^ carry)) && bBit){ //Carry bit is true next
if(carry)
answer |= bitMask;
carry = true;
}else{
if(carry)
answer |= bitMask;
carry = false;
}
}
printBinaryNumber(answer, 8);
}
void doProblem(const int& a, const int& b, const char& sign, const int& result, const int& numBits){
if(sign == '+'){
cout << ' '; printCarryBits(a, b, numBits); cout << endl;
}else{
cout << ' '; printBorrowBits(a, b, numBits); cout << endl;
}
cout << ' '; printBinaryNumber(a, numBits); cout << endl;
cout << sign; printBinaryNumber(b, numBits); cout << endl;
cout << "----------" << endl;
cout << ""; printBinaryNumber(result, numBits + 1); cout << " = " << result;
cout << endl;
}
int main(){
Number a = 0b110;
Number b = 0b011;
cout<< a << endl << b << endl;
doProblem(a, b, '+', a + b, NUM_BITS);
doProblem(a, b, '-', a - b, NUM_BITS);
doProblem(-a, b, '+', -a + b, NUM_BITS);
doProblem(a, b, '-', -a - b, NUM_BITS);
return 0;
}

Big O(n^2) Ascending Sort

0 likes • Nov 18, 2022 • 6 views
C++
#include <iostream>
using namespace std;
int main() {
int arr[5];
for(int i = 0; i < 5; i++) {
arr[i] = i;
}
for(int i = 0; i < 5; i++) {
cout << "Outputting array info at position " << i + 1 << ": " << arr[i] << endl;
}
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout << endl;
for(int i = 0; i < 5; i++) {
cout << "Outputting sorted array info at position " << i + 1 << ": " << arr[i] << endl;
}
return 0;
}

Compute Volume of Cylinder

0 likes • Nov 18, 2022 • 0 views
C++
/*
Algorithm:
Step 1: Get radius of the cylinder from the user and store in variable r
Step 2: Get height of the cylinder from the user and store in variable h
Step 3: Multiply radius * radius * height * pi and store in v
Step 4: Display the volume
*/
#include <iostream>
using namespace std;
int main()
{
float r; //define variable for radius
float h; //define variable for height
float v;
float pi;
pi=3.1416;
cout<<"Enter radius:";
cin>>r;
cout<<"Enter height:";
cin>>h;
v=r*r*h*pi; //compute volume
cout<<"Radius:"<<r<<"\tHeight:"<<h<<endl; //display radius and height
cout<<"\n************************\n";
cout<<"Volume:"<<v<<endl;//display volume
return 0;
}

Stock Options Analyzer

0 likes • Nov 18, 2022 • 0 views
C++
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
int main() {
string tickerName;
int numOfContracts;
float currentOptionValue;
cout << "Enter a stock ticker: ";
getline(cin, tickerName);
cout << "Enter the current number of " << tickerName << " contracts you are holding: ";
cin >> numOfContracts;
cout << "Enter the current price of the option: ";
cin >> currentOptionValue;
cout << "The value of your " << tickerName << " options are: $" << (currentOptionValue * 100.00) * (numOfContracts);
cout << endl;
return 0;
}

GCD using Stein's Algorithm

0 likes • Jun 30, 2023 • 2 views
C++
// Iterative C++ program to
// implement Stein's Algorithm
//#include <bits/stdc++.h>
#include <bitset>
using namespace std;
// Function to implement
// Stein's Algorithm
int gcd(int a, int b)
{
/* GCD(0, b) == b; GCD(a, 0) == a,
GCD(0, 0) == 0 */
if (a == 0)
return b;
if (b == 0)
return a;
/*Finding K, where K is the
greatest power of 2
that divides both a and b. */
int k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
/* Dividing a by 2 until a becomes odd */
while ((a & 1) == 0)
a >>= 1;
/* From here on, 'a' is always odd. */
do
{
/* If b is even, remove all factor of 2 in b */
while ((b & 1) == 0)
b >>= 1;
/* Now a and b are both odd.
Swap if necessary so a <= b,
then set b = b - a (which is even).*/
if (a > b)
swap(a, b); // Swap u and v.
b = (b - a);
} while (b != 0);
/* restore common factors of 2 */
return a << k;
}
// Driver code
int main()
{
int a = 12, b = 780;
printf("Gcd of given numbers is %d\n", gcd(a, b));
return 0;
}

Literal Bruh

0 likes • Jul 30, 2023 • 5 views
C++
//Constant prefix notation solver using bruh
//Could make it infix or postfix later
#include<string>
#include<vector>
#include<iostream>
std::vector<long double> bruhBuff;
long double operator ""bruh(long double a){
bruhBuff.push_back(a);
return a;
}
long double operator ""bruh(const char op){
if(bruhBuff.size() < 2) throw "Bruh weak";
long double b = bruhBuff.back();
bruhBuff.pop_back();
long double a = bruhBuff.back();
bruhBuff.pop_back();
switch(op){
case (int)('+'):
return a + b;
case (int)('-'):
return a - b;
case (int)('*'):
return a * b;
case (int)('/'):
return a / b;
}
return 69l;
}
int main(){
1.0bruh;
2.0bruh;
std::cout << '+'bruh << std::endl;
return 0;
}