Initial commit
This commit is contained in:
commit
027d896835
5 files changed
+161
No files matched your search
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+45
@@ -0,0 +1,45 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "BitRange-Calculator"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
|
||||
dependencies = [
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.46"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "BitRange-Calculator"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
num-bigint = "0.4.6"
|
||||
num-traits = "0.2.19"
|
||||
@@ -0,0 +1,43 @@
|
||||
# BitRange Calculator
|
||||
|
||||
Simple command-line tool written in Rust that calculates and formats the maximum, signed, and unsigned ranges of integers based on a given bit size. It helps to understand the range of values that can be represented in binary for a specified number of bits.
|
||||
|
||||
## Features
|
||||
|
||||
- Calculate the maximum value for an unsigned integer based on the number of bits.
|
||||
- Calculate the signed integer range (both positive and negative) based on the number of bits.
|
||||
- Format large numbers with commas for easy readability.
|
||||
- Supports flexible bit sizes (positive integers only).
|
||||
|
||||
## Download
|
||||
|
||||
You can download pre-compiled executables here: [Click to go to the Releases Tab](https://github.com/Lncvrt/BitRange-Calculator/releases/latest)
|
||||
|
||||
## Usage
|
||||
|
||||
The program prompts you to enter the number of bits. Based on the input, it calculates the following:
|
||||
|
||||
- **Maximum value for an unsigned integer**.
|
||||
- **Formatted maximum value for an unsigned integer** with commas for readability.
|
||||
- **Signed integer range** showing the minimum and maximum values.
|
||||
- **Formatted signed range** with commas for readability.
|
||||
|
||||
Examples:
|
||||
|
||||
16-Bit Calculation:
|
||||
```
|
||||
Max value for a 16-bit unsigned integer: 65535 (65,535)
|
||||
Signed range for a 16-bit integer: -32768 to 32767 (-32,768 to 32,767)
|
||||
```
|
||||
|
||||
32-Bit Calculation:
|
||||
```
|
||||
Max value for a 32-bit unsigned integer: 4294967295 (4,294,967,295)
|
||||
Signed range for a 32-bit integer: -2147483648 to 2147483647 (-2,147,483,648 to 2,147,483,647)
|
||||
```
|
||||
|
||||
64-Bit Calculation:
|
||||
```
|
||||
Max value for a 64-bit unsigned integer: 18446744073709551615 (18,446,744,073,709,551,615)
|
||||
Signed range for a 64-bit integer: -9223372036854775808 to 9223372036854775807 (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
|
||||
```
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
use num_bigint::{BigInt, BigUint};
|
||||
use num_traits::One;
|
||||
use std::io::{self, Write};
|
||||
|
||||
fn format_number(n: &impl ToString) -> String {
|
||||
let num_str = n.to_string();
|
||||
let mut result = String::new();
|
||||
let mut count = 0;
|
||||
|
||||
for c in num_str.chars().rev() {
|
||||
if count == 3 {
|
||||
result.push(',');
|
||||
count = 0;
|
||||
}
|
||||
result.push(c);
|
||||
count += 1;
|
||||
}
|
||||
|
||||
result.chars().rev().collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut input = String::new();
|
||||
print!("Enter the number of bits: ");
|
||||
io::stdout().flush().unwrap();
|
||||
|
||||
io::stdin().read_line(&mut input).unwrap();
|
||||
let bits: usize = match input.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => {
|
||||
println!("Invalid input. Please enter an integer.");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if bits <= 0 {
|
||||
println!("Invalid bit size. Must be greater than 0.");
|
||||
return;
|
||||
}
|
||||
|
||||
let max_value = BigUint::one() << bits;
|
||||
let max_value = max_value - BigUint::one();
|
||||
|
||||
let signed_max = BigUint::one() << (bits - 1);
|
||||
let signed_max = signed_max - BigUint::one();
|
||||
|
||||
let signed_min = BigInt::one() << (bits - 1);
|
||||
let signed_min = -signed_min;
|
||||
|
||||
println!(
|
||||
"Max value for a {}-bit unsigned integer: {} ({})",
|
||||
bits,
|
||||
max_value,
|
||||
format_number(&max_value)
|
||||
);
|
||||
println!(
|
||||
"Signed range for a {}-bit integer: {} to {} ({} to {})",
|
||||
bits,
|
||||
signed_min,
|
||||
signed_max,
|
||||
format_number(&signed_min),
|
||||
format_number(&signed_max)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user