-3

How can I create a type with custom byte size in c/c++?

  • I want to create a int with 128 or more bytes
  • I have use struct but it can only create in range 1 to 32 bytes
  • I don't want to use array, vectors or anything like this
  • I just want to create a int with more than 128 bytes
  • Generally, you **can't** period. Or do you mean **bytes** or bits? – Antti Haapala -- Слава Україні Sep 18 '20 at 14:08
  • 4
    Write your own compiler with that custom type support. – KamilCuk Sep 18 '20 at 14:09
  • What about `std::bitset`? – vahancho Sep 18 '20 at 14:10
  • "I have use struct but it can only create in range 1 to 32 bytes" what did you do? Why is 32 bytes the limit? – 463035818_is_not_an_ai Sep 18 '20 at 14:11
  • " what did you do? Why is 32 bytes the limit? " compiler said that – Ahmad Mahmoudi Sep 18 '20 at 14:13
  • "Write your own compiler with that custom type support." I just ask. – Ahmad Mahmoudi Sep 18 '20 at 14:14
  • "Generally, you can't period. Or do you mean bytes or bits?"I mean Bytes – Ahmad Mahmoudi Sep 18 '20 at 14:15
  • 1
    No, the compiler most definitely did not say there was a limit of 32 bytes, unless you are using some ancient dinosaur stuff. – Lundin Sep 18 '20 at 14:16
  • 1
    *I don't want to use array, vectors or anything like this* -- So what magic are you going to use to create something that can't exist, unless you write the code to do it? That code is going to use some sort of aggregate in some way, no? – PaulMcKenzie Sep 18 '20 at 14:16
  • "No, the compiler most definitely did not say there was a limit of 32 bytes, unless you are using some ancient dinosaur stuff" this is what ide say Width of bit-field 'theInt' (128 bits) exceeds the width of its type; value will be truncated to 32 bits – Ahmad Mahmoudi Sep 18 '20 at 14:21
  • bits and bytes are not the same. You used the wrong term of bytes then. For bytes you could make an array of GB in size. Look into big math libraries on how they are implemented. – drescherjm Sep 18 '20 at 14:22
  • "So what magic are you going to use to create something that can't exist, unless you write the code to do it? That code is going to use some sort of aggregate in some way, no?"....................... I just think about this. asm language can do it, it can crate any type with any bytes that you want. so c++ can use asm in itself. I just thought can I create a large int using asm in c++ or not. – Ahmad Mahmoudi Sep 18 '20 at 14:24
  • You can implement such a library in c++ without resorting to asm. – drescherjm Sep 18 '20 at 14:25
  • Related: [https://stackoverflow.com/questions/12988099/big-numbers-library-in-c](https://stackoverflow.com/questions/12988099/big-numbers-library-in-c) – drescherjm Sep 18 '20 at 14:26
  • thanks for all of your comments. forgive me if I asked in wrongly case. I have some big_int libraries and they are using string or vectors for doing that. they are so slow. for this I asked this question. Thank you all. – Ahmad Mahmoudi Sep 18 '20 at 14:30
  • 1
    @احمدمحمودی Yes, BigInt numbers are by necessity slower than native integers. Because most operations are much more complicated and are not implemented directly in hardware. – bitmask Sep 18 '20 at 14:32
  • 2
    @احمدمحمودیImagine how you would solve this in assembly. You will necessarily be limited by the architecture in terms of operations you can perform on your large integer. Operations on it will have to decompose the value into smaller values the CPU can accomodate and iterate over these components. This is what the "slow" Bigint libraries are doing. It won't be much faster in assembly because ultimately the limitation is at the hardware level. – François Andrieux Sep 18 '20 at 14:35
  • I would not expect to easily beat the performance of established big integer libraries. These have been worked on and optimized for years. – drescherjm Sep 18 '20 at 14:37
  • See this: https://gmplib.org/ – i486 Sep 18 '20 at 14:39
  • 2
    @احمدمحمودی -- Libraries such as [gmp](https://gmplib.org/pi-with-gmp) would have implemented what you're looking for already. – PaulMcKenzie Sep 18 '20 at 14:43
  • I believe the fundamental issue is that you need a processor that can perform arithmetic on 128-bit quantities (as well as memory that can handle a 128-bit data bus). This is the only way you'll get fast math with large numbers. Your bottlenecks will be loading and storing to memory. As long as you keep values in memory, you'll have the high speed. – Thomas Matthews Sep 18 '20 at 15:40
  • You could use `std::bitset` to contain the number, but you'll have to write all the math functions in terms of the `std::bitset`. For example, you'll have to decompose the `std::bitset` into pieces small enough for the processor to efficiently handle (this is what happens in assembly language), then perform the appropriate math. – Thomas Matthews Sep 18 '20 at 15:42
  • If you are writing your own math functions, I recommend using `uint8_t` as your data type. It's small enough to keep your program simple. For example, take a 32-bit integer, and decompose into 8-bit units and perform all the math (such as add with carry) on the 8-bit quantities. – Thomas Matthews Sep 18 '20 at 15:44

1 Answers1

6

I just want to create a int with more than 128 bytes

You just want something that is not possible in the languages that you've chosen.

It is simply not possible to define custom integer types in these languages. Classes (structs) are the only custom types that can be defined (and type aliases if you count those).

What you can do to represent such large integers is a large array of smaller integer.

I don't want to use any Array

Then you've run out of options.

... at least for now. In future, if a proposal such as n2472 is adopted in to C++ standard, this may become possible.

in asm we can create any type with any bytes I just ask about this can I use asm in c++ for creating that?

If you write your program in assembly, then you can do whatever your assembler allows. But you cannot introduce types into C++ in asm declarations.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • in asm we can create any type with any bytes I just ask about this can I use asm in c++ for creating that? – Ahmad Mahmoudi Sep 18 '20 at 14:18
  • "Then you've run out of options." Technically not true; `template struct BigInt { BigInt next; std::byte data; }; template <> struct BigInt<1> { std::byte data; };` would be an almost arbitrary number without using an array. But it makes no sense to not just use `std::array` or perhaps `std::size_t[N]`. – bitmask Sep 18 '20 at 14:29
  • @احمدمحمودی You can do that in C++. You define new types by defining classes, there is no need to involve assembly. But any reasonable implementation of a very large integer type will use an array in some way. So if arrays are out of the question it becomes very hard to write a reasonable large integer type. – François Andrieux Sep 18 '20 at 14:31
  • @FrançoisAndrieux Oh wow. I didn't even know that asm was a stadard declaaration :D Indeed, if one uses such assembler, then one could write their programs in assembly in C++. – eerorika Sep 18 '20 at 14:31
  • @eerorika I have know this and for this I have asked it. – Ahmad Mahmoudi Sep 18 '20 at 14:35
  • Neat proposal. I hadn't heard of it before. – François Andrieux Sep 18 '20 at 14:40