-3

I am new to programming and I'm learning bit shifting.

Suppose I have 2 long long bytes given

long long bytes1 = b11111111; long long bytes2 = b10000000;

long long result;

I want to concat the 2 bytes so that the result variable will hold: = 1000000011111111

Is there a way on how to concat this in my specific order of bytes?

  • Any [good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would have told you how to use the bitwise operators. Please read a book, then come back here and update your question with what you have tried and how it didn't work. Also please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jan 04 '18 at 14:36
  • `b0000000`. The extra zero's without a `1` holds no significance to the value – smac89 Jan 04 '18 at 14:37
  • 1
    *I have 2 long long bytes* I think you may be a bit confused; a byte is always 8 bits, but `long long` is typically 64 bits. "long long bytes" doesn't make any sense. – 0x5453 Jan 04 '18 at 14:59

2 Answers2

0

Well, if it's only 8 bits wide, you don't need datatype long long. uint8_t is exactly 8 bits wide.

uint8_t bytes1 = b11111111;
uint8_t bytes2 = b10000000;

The result will be 16 bits wide, like uint16_t.

uint16_t result1 = (bytes2 << 8) | bytes1; // will be b1000000011111111
uint16_t result2 = (bytes1 << 8) | bytes2; // will be b1111111110000000

The << operator shifts bits to the left, while the | operator does a binary OR operation (though in this case, a + would have worked just as well).

Thomas Hilbert
  • 3,559
  • 2
  • 13
  • 33
  • 1
    You may want to prefer the standard types `uint8_t` for 8 bits and `uint16_t` for 16 bit numbers. The `unsigned char` is *at least* 8 bits, could be more. An `unsigned short int` is at least 16 bits, could be 32. – Thomas Matthews Jan 04 '18 at 14:44
-2

I think you can use the LARGEINTEGER. For example:

LONGLONG byte1 = 10000000;
LONGLONG byte2 = 11111111;
LARGE_INTEGER largeInteger;
largeInteger.LowPart = byte1;
largeInteger.HighPart = byte2;
cout<< largeInteger.HighPart << largeInteger.LowPart;

or you can use strings

LONGLONG llByte1 = 10000000;
LONGLONG llByte2 = 11111111;
string sBytes = std::to_string(llByte1);
sBytes.append(std::to_string(llByte2));
LONGLONG llBytes = atoll(sBytes.c_str());
cout << endl << "llBytes "<<llBytes << endl;