1

In Abseil library absl::uint128 big = absl::MakeUint128(1, 0); this represents 2^64 , but i don't understand what does '1' and '0' mean here. Can someone explain me how the number is actually formed ?

Anuj
  • 11
  • 2
  • It looks like `1` is the higher 64 bits and `0` is the lower 64 bits. – MikeCAT Jul 13 '22 at 15:44
  • yes, it is. but how exactly they combine together to form 2^64 ? – Anuj Jul 13 '22 at 15:46
  • 1
    the `0` is the value of the lower 64 bits and the `1` is the value of upper 64 bits. When combined that means you have `10..62times..0` which is 2^64 – NathanOliver Jul 13 '22 at 15:48
  • https://abseil.io/docs/cpp/guides/numeric – L. Scott Johnson Jul 13 '22 at 15:49
  • How is the number 2^64 written in binary? can you write down the number 2^64 using 128 bits, and put a space in the middle (between bits 64 and 65)? – user253751 Jul 13 '22 at 15:50
  • `absl::MakeUint128(a, b)` creates the 128 bit number `a * 2^64 + b`. So `absl::MakeUint128(1, 0)` creates `1 * 2^64 + 0`. That's 2^64. – Drew Dormann Jul 13 '22 at 15:50
  • @ANUJ It's not explicitly documented by by convention that's what it means. When you have a wrapper for a type that takes and `high` and `low` for a larger type then formula is `2^bit_width_of_final_type * high + low` where `high` and `low` will be types that have a size of `bit_width_of_final_type / 2` – NathanOliver Jul 13 '22 at 16:19
  • @NathanOliver thanks for this information, actually i didn't know about this earlier, so it wasn't obvious for me – Anuj Jul 13 '22 at 16:26

2 Answers2

2

absl::MakeUint128(x, y); constructs a number equal to 2^64 * x + y

And see https://abseil.io/docs/cpp/guides/numeric

L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
1

How? In any possible way. But there is a very simple way to make it. You may already know how to do arithmetic with one digit numbers in base ten, right? Then you also know to use this arithmetic to get arithmetic of two digit numbers in base 10, right? Be aware that this then gives you an arithmetic of one digit numbers in base 100 (just consider '34' or '66' as a single symbols).

Your computer knows how to make arithmetic of one number digit in base 2^64, so it makes the same extension that you use to get in base 10 to get arithmetic of two digit numbers in base 2^64. This then leads to an arithmetic in base 2^128, or an arithmetic of 128 digits numbers in base 2.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69