2

IN C Programming, how do I combine (note: not add) two integers into one big integer? So if i have

int a = 8 int b = 6

in binary it would be int a = 1000 int b = 0110

so combined it would be = 01101000

Rob
  • 14,746
  • 28
  • 47
  • 65

4 Answers4

3

You would use a combination of the << shift operator and the bitwise | operator. If you are trying to build an 8-bit value from two 4-bit inputs, then:

int a = 8;
int b = 6;

int result = (b << 4) | a;

If you are trying to build a 32-bit value from two 16-bit inputs, then you would write

result = (b << 16) | a;

Example:

#include <stdio.h>

int main( void )
{
  int a = 8;
  int b = 6;

  printf( "a = %08x, b = %08x\n", (unsigned int) a, (unsigned int) b );

  int result = (b << 4) | a;

  printf( "result = %08x\n", (unsigned int) result );

  result = (b << 8) | a;

  printf( "result = %08x\n", (unsigned int) result );

  result = (b << 16) | a;

  printf( "result = %08x\n", (unsigned int) result );

  return 0;
}

$ ./bits
a = 00000008, b = 00000006
result = 00000068
result = 00000608
result = 00060008
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • If i do: `int8_t t1 = -100; int8_t t2 = 3; int16_t r = (t2 << 8) | t1;`, r now gets the value of -100. I am completely at a loss to what;s happening here. It should be 924, shouldn't it?? – Samuël Visser Jul 02 '22 at 13:26
  • @SamuëlVisser - Try declaring `t1` and `t2` as `int16_t` - since `t1` and `t2` are both `int8_t`, I suspect the `t2 << 8 | t1` expression is yielding an `int8_t` result, which in this case would be `(00000000 | 10011100) == 100111000`, which gets sign-extended to `11111111100111000` (`-100`). – John Bode Jul 02 '22 at 14:37
  • Hi thx for nice typ, and how can I convert from two values from 16-bit to 8-bit? and when I use big value from example 255, I have in printf `printf( "a = %08x, b = %08x\n", (unsigned int) a, (unsigned int) b );` hexa , it is posible print binari? – user2219071 Jan 27 '23 at 07:45
2

You can do it as follow using binary mask & 0x0F and bit translation <<:

int a = 0x08
int b = 0x06
int c = (a & 0x0F) + ((b & 0x0F) << 4 )

I hope that it helped

Update 1:

As mentionned in the comment addition + or binary or | are both fine.

What is important to highlight in this answer is the mask & 0x0F, I strongly recommand to use this kind of mecanism to avoid any overflow.

White
  • 71
  • 1
  • 13
  • 2
    You can also use `|` (bitwise OR) instead of +. No difference here because there's no carry, but it probably conveys the intent a bit more clearly. – nanofarad Sep 28 '21 at 16:29
  • how would i go about writing the code as a whole –  Sep 28 '21 at 16:58
1

you could use or operator.

int a = 8 ;
int b = 6 ;

int c = (a << 8) | b;
  • You're shifting too much, OP says the desired output should be 0b01101000 == 0x68 – yano Sep 28 '21 at 16:38
  • Actually no. if you want to create 16-bit int variable with two 8-bit data, you must shif 8 bit. If not, you actually create less than 16-bit number. For example your new number can be so small that it can be fit in 8-bit. For your example, 0x68 is not a 16-bit number. it is 8-bit number. – Ahmet Dundar Sep 28 '21 at 16:50
  • In OP's example, they are shifting by only 4 bits. But I agree that this does not make sense, because OP is explicitly asking for a 16-bit result. – Andreas Wenzel Sep 28 '21 at 16:55
  • "in binary it would be int a = 1000 int b = 0110 so combined it would be = 01101000" Verbatim from OP, maybe that's been edited since you answered, I don't know, but your answer will produce 0x608 == 0x1000 0000 0110 – yano Sep 28 '21 at 16:56
  • Yes you are almost right but you and OP miss something. the question is create 16-bit number. OP's example actually wrong because it is still 8-bit number. For another example; If I shift one of two variable left by 1-bit and combine it with other, I do create one big number, right? int c = a << 1 | b. After this process, c's variable is 10110 or 0x16. It is bigger than both of the variables but smaller than OP's result. So is my c variable small or big? But you would be right if the question is create bigger number combining any two integers by using bit-wise or something. – Ahmet Dundar Sep 28 '21 at 17:13
1

You can use the bit-shift operator << to move the bits into the correct position:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main()
{
    uint8_t a = 8;
    uint8_t b = 6;

    uint16_t c = (b << 4) | a;

    printf( "The result is: 0x%" PRIX16 "\n", c );
}

This program will print the following:

The result is: 0x68

Note that this program uses fixed-width integer types, which are recommended in this situation, as you cannot rely on the size of an int or unsigned int to have a certain width.

However, there is no need for the result to be 16-bits, if you are only shifting one value by 4 bits, as you are doing in your example. In that case, an integer type with a width of 8-bits would have been sufficient. I am only using 16-bits for the result because you explicitly asked for it.

The macro PRIX16 will probably expand to "hX" or "X" on most platforms. But it is still recommended to use this macro when using fixed-width integer types, as you cannot rely on %hX or %X being the correct format specifier for uint16_t on all platforms.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • appreciate you thanks! –  Sep 28 '21 at 17:10
  • is there a way to convert it to binary instead of getting 0x68? –  Sep 28 '21 at 17:13
  • @XavierMejia: That is possible, but not very easy: [Is there a printf converter to print in binary format?](https://stackoverflow.com/q/111928/12149471) – Andreas Wenzel Sep 28 '21 at 17:15
  • im sorry i mean conert hexadecimal to an integer –  Sep 28 '21 at 17:31
  • @XavierMejia: Do you mean convert an `integer` to a string in binary representation? For that, you can use `sprintf` instead of `printf`, using the methods described in the question that I linked to. Or are you asking the program to print `01101000` instead of `0x68`? That is exactly what the question does that I linked to. – Andreas Wenzel Sep 28 '21 at 18:32
  • @XavierMejia: Or are you asking how to convert a string, which contains a number in hexadecimal format, into an integer? For that, you can use the function [`strtol`](https://en.cppreference.com/w/c/string/byte/strtol). – Andreas Wenzel Sep 28 '21 at 20:37