0

is it possible to divide for example an integer in n bits?

For example, since an int variable has a size of 32 bits (4 bytes) is it possible to divide the number in 4 "pieces" of 8 bits and put them in 4 other variables that have a size of 8 bits?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • There will be many dupes if you ask how to split an `int` into n bytes. Note though that in C the size of an `int` isn't well defined. As for bit ops themselves, maybe you need to lookup things like "masking", and follow a few tutorials on bit operations on integers. – Maarten Bodewes May 07 '22 at 18:25
  • @MaartenBodewes or just answer his question – Fredrik May 07 '22 at 18:26
  • 1
    @Fredrik No, that's not how this Q/A site works. I'm happy to steer in the right direction, but this question clearly has been answered many times before, and the asker is much more helped by looking at operators such as `<<`, `&` and `^` in a good tutorial. – Maarten Bodewes May 07 '22 at 18:28
  • @Fredrik Tried an explanatory answer instead, but it still points to other questions and C books. – Maarten Bodewes May 07 '22 at 18:56
  • 1
    Please add solution as an answer and accept the same instead of editing into the question – Suraj Rao Jun 23 '22 at 09:19

2 Answers2

0

I solved using unsigned char *pointer pointing to the variable that I want to analyze bytes, something like this:

int x = 10;
unsigned char *p = (unsigned char *) &x;
//Since my cpu is little endian I'll print bytes from the end
for(int i = sizeof(int) - 1; i >= 0; i--)
    //print hexadecimal bytes
    printf("%.2x ", p[i]);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-1

Yes, of course it is. But generally we just use bit operations directly on the bits (called bitops) using bitwise operators defined for all discrete integer types.

For instance, if you need to test the 5th least significant bit you can use x &= 1 << 4 to have x just to have the 5th bit set, and all others set to zero. Then you can use if (x) to test if it has been set; C doesn't use a boolean type but assumes that zero is false and any other value means true. If you store 1 << 4 into a constant then you have created a "(bit) mask" for that particular bit.

If you need a value 0 or 1 then you can use a shift the other way and use x = (x >> 4) & 1. This is all covered in most C books, so I'd implore you to read about these bit operations there.


There are many Q/A's here how to split integers into bytes, see e.g. here. In principle you can store those in a char, but if you may require integer operations then you can also split the int into multiple values. One problem with that is that an int is just defined to at least store values from -32768 to 32767. That means that the number of bytes in an int can be 2 bytes or more.


In principle it is also possible to use bit fields but I'd be hesitant to use those. With an int you will at least know that the bits will be stored in the least significant bits.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • Actually I solved like this: 'int n = 2; void *p = &n;' because the void *p can see only 1 byte at a time so if I need to divide the int in 4 pieces of 8 bits I can access those pieces just by doing *(p + 0) to see the first 8 bits, *(p + 1) to see the second 8 bits, until p + 3, but I noticed that it uses little endian so *(p + 0) actually is the last 8 bits and so on. Thanks anyway –  May 07 '22 at 19:39
  • You'll probably get some indication from somebody that this isn't portable, but yeah, for bytes you can possibly use pointer arithmetic. I'm not a fan of that though, and using variables (i.e. registers) is probably faster. – Maarten Bodewes May 07 '22 at 19:59
  • Can you give me an example please? And why wouldn't it be portable? –  May 08 '22 at 01:57
  • I've given examples in the answer, and the portability is because the bitwise representation of a C int is not well defined. – Maarten Bodewes May 08 '22 at 13:41