Skip to content

Instantly share code, notes, and snippets.

@shivupa
Created December 6, 2022 03:01
Show Gist options
  • Save shivupa/199da68de90eb634960f0a7d93584bac to your computer and use it in GitHub Desktop.
Save shivupa/199da68de90eb634960f0a7d93584bac to your computer and use it in GitHub Desktop.
bitset signed vs unsigned
#include <bitset>
#include <cmath>
#include <cstdint>
#include <iostream>
void demo_8() {
const unsigned bit_kind = 8;
uint8_t a_ui = 129;
int8_t a_i = 129;
uint8_t a_ui_from_i = 127;
std::bitset<bit_kind> b;
std::cout << " bitkind " << bit_kind << std::endl;
std::cout << " std::pow(2,7) + 1 = " << std::pow(2, 7) + 1 << std::endl;
b = a_ui;
std::cout << " uint" << bit_kind << "_t " << static_cast<int>(a_ui);
std::cout << " bitset<bitkind> " << b;
std::cout << std::endl;
b = a_i;
std::cout << " int" << bit_kind << "_t " << static_cast<int>(a_i);
std::cout << " bitset<bitkind> " << b;
std::cout << std::endl;
b = a_ui_from_i;
std::cout << " uint_t from int_t " << static_cast<int>(a_ui_from_i);
std::cout << " bitset<bitkind> " << b;
std::cout << std::endl;
std::cout << std::endl;
}
void demo_64() {
const unsigned bit_kind = 64;
uint64_t a_ui = 9223372036854775809;
int64_t a_i = 9223372036854775809;
uint64_t a_ui_from_i = 9223372036854775807;
std::bitset<bit_kind> b;
std::cout << " bitkind " << bit_kind << std::endl;
uint64_t pow_val = std::pow(2, 63);
pow_val += 1;
std::cout << " std::pow(2,63) + 1 = " << std::fixed << pow_val << std::endl;
b = a_ui;
std::cout << " uint" << bit_kind << "_t " << a_ui;
std::cout << " bitset<bitkind> " << b;
std::cout << std::endl;
b = a_i;
std::cout << " int" << bit_kind << "_t " << a_i;
std::cout << " bitset<bitkind> " << b;
std::cout << std::endl;
b = a_ui_from_i;
std::cout << " uint_t from int_t " << a_ui_from_i;
std::cout << " bitset<bitkind> " << b;
std::cout << std::endl;
std::cout << std::endl;
}
int main() {
std::cout << "Highest occ orbtial bug!" << std::endl;
demo_8();
demo_64();
return 0;
}
@shivupa
Copy link
Author

shivupa commented Dec 6, 2022

Highest occ orbtial bug!
 bitkind 8
 std::pow(2,7) + 1 = 129
 uint8_t 129 bitset<bitkind> 10000001
 int8_t -127 bitset<bitkind> 10000001
 uint_t from int_t 127 bitset<bitkind> 01111111

 bitkind 64
 std::pow(2,63) + 1 = 9223372036854775809
 uint64_t 9223372036854775809 bitset<bitkind> 1000000000000000000000000000000000000000000000000000000000000001
 int64_t -9223372036854775807 bitset<bitkind> 1000000000000000000000000000000000000000000000000000000000000001
 uint_t from int_t 9223372036854775807 bitset<bitkind> 0111111111111111111111111111111111111111111111111111111111111111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment