跳到正文

个人记录

SHABI v0.1

Splay_trees约 8 分钟
原文
文章信息与版本

文章信息

发布
2024年2月18日
收录
2025年12月12日
更新
2026年10月29日
篇幅
约 8 分钟

版本

  1. v1当前
C++
/*
	Simple High Accuracy Bitset Integer
	(SHABI) v0.1
	1.Added basic operators:
	i)   Arithmetic operators + - * / %
	ii)  Boolean operators > < >= <=
	iii) Miscellaneous operators lowbit(),HighBit(),Signed()
	
*/
/*
	I started this project because bitset is really
	useful and bitset supports lots of bit operators on its own.
	Better than re-writing SHADL in arrays.
	It supports negative numbers and it needs less code than SHADL.
	
	But it also has some side effects:
	Because it is based on binary so it needs many bits to describe
	a number so it cannot calculate huge numbers because it's time 
	complexity is so huge despite on theoretically O(n/w).
	
	I am not going any further on it.
*/
#include <iostream>
#include <cstdio>
#include <bitset>
#include <stack>
#define SIZE 256
using namespace std;
typedef bitset<SIZE> BigInt;
//Arithmetic operators(1)
BigInt operator+(BigInt a,BigInt b)
{
	//It looks unnecessary to avoid recursing,
	//but it is very important to do so.
	BigInt sum[2],carry[2];
	carry[0]=(a&b)<<1;
	sum[0]=a^b;
	for(bool i=0;; i=!i)
	{
		if(carry[i].none())
		{
			return sum[i];
		}
		sum[!i]=sum[i]^carry[i];
		carry[!i]=(sum[i]&carry[i])<<1;
	}
}
BigInt operator-(const BigInt &b)
{
	return ~b+1;
}
BigInt operator-(const BigInt &a,const BigInt &b)
{
	return a+(-b);
}
//Boolean operators
bool operator!(const BigInt &b)
{
	return !b.none();
}
bool Signed(const BigInt &b)
{
	return b[b.size()-1]==1;
}
bool operator>(BigInt a,BigInt b)
{
	bool minor=0;
	if(Signed(a) && !Signed(b)) return 0;
	if(Signed(b) && !Signed(a)) return 1;
	if(Signed(a) &&  Signed(b))
	{a=-a,b=-b,minor=1;}
	for(int i=SIZE-1; ~i; i--) //~i -> i>=0 here
	{
		if(a[i]>b[i]) return minor?0:1;
		if(a[i]<b[i]) return minor?1:0;
	}
	return 0;
}
bool operator<(BigInt a,BigInt b)
{
	bool minor=0;
	if(Signed(a) && !Signed(b)) return 1;
	if(Signed(b) && !Signed(a)) return 0;
	if(Signed(a) &&  Signed(b))
	{a=-a,b=-b,minor=1;}
	for(int i=SIZE-1; ~i; i--) //~i -> i>=0 here
	{
		if(a[i]>b[i]) return minor?1:0;
		if(a[i]<b[i]) return minor?0:1;
	}
	return 0;
}
bool operator<=(const BigInt &a,const BigInt &b)
{
	return !(a>b);
}
bool operator>=(const BigInt &a,const BigInt &b)
{
	return !(a<b);
}

//Arithmetics operator(2)
BigInt operator*(const BigInt &a,const BigInt &b)
{
	//This is smul in OI
	//similar to qpow
	BigInt res,c=a,d=b;
	while(!c.none())
	{
		if(c[0]) res=res+d;
		d=d+d,c>>=1;
	}
	return res;
}
//Miscellaneous
BigInt lowbit(const BigInt &b)
{
	return b&(~b+1);
}
int HighBit(BigInt b)
{
	BigInt k;
	while(!b.none())
	{
		k=lowbit(b);
		b=b-k;
	}
	return k._Find_first();
}
BigInt operator/(BigInt a,BigInt b)
{
	if(b==BigInt(0))
	{
		throw "error:Dividing by zero.";
	}
	bool a_minor_0=0,b_minor_0=0;
	BigInt rem=a,tmp=b,res;
	if(rem<BigInt(0))
	{
		a_minor_0=1;
		rem=-rem;
	}
	if(b<BigInt(0))
	{
		b_minor_0=1;
		b=-b;
	}
	int bit=0;
	while(rem>=b)//-> rem >= 0
	{
		tmp=b;
		bit=HighBit(rem);
		tmp<<=bit;
		while(tmp>rem) tmp>>=1,bit--;
		res[bit]=1,rem=rem-tmp;
	}
	if(a_minor_0^b_minor_0) res=-res;
	return res;
}
BigInt operator%(BigInt a,BigInt b)
{
	return a-b*(a/b);
}
//Fast I/O
void read(BigInt &Data)
{
	char c=0;
	bool get=0;
	while(!isdigit(c))
	{
		c=getchar();
		if(c=='-') get=1;
	}
	while(isdigit(c))
	{
		Data=(Data<<3)+(Data<<1);
		Data=Data+(c-'0');
		c=getchar();
	}
	if(get) Data=~Data+1;
	return;
}
//Cast
long long to_llong(const BigInt &b)
{
	BigInt tmp=b;
	bool get=0;
	long long res=0;
	if(tmp[63]==1)
	{
		get=1;
		tmp=tmp-1;
		tmp=~tmp;
	}
	for(int i=0; i<63; i++)
	{
		res+=1ll*tmp[i]<<i;
	}
	if(get) res=-res;
	return res;
}
void print(BigInt a)
{
	if(Signed(a))
	{
		putchar('-');
		a=-a;
	}
	constexpr unsigned long long ACC = 1e16;
	stack<unsigned long long> stk;
	while(!a.none())
	{
		stk.push((a%ACC).to_ullong());
		a=a/ACC;
	}
	while(!stk.empty())
	{
		printf("%16llu",stk.top());
		stk.pop();
	}
}
int main()
{
	BigInt a,b;
	freopen("C:\\users\\thomas\\desktop\\Help\\c\\test.in","r",stdin);
	read(a);
	cout<<HighBit(a);
	return 0;
}
原文