Convert uint64_t to 8 byte little endian long [duplicate] - c++

This question already has answers here:
How do I convert a value from host byte order to little endian?
(7 answers)
Closed 6 years ago.
I have a variable called length that is of type uint64_t. I want to convert it to 8 byte little endian long and I'm very confused about these data types.
Can someone guide me on how to make this conversion?

Well, there is Boost.Endian.
#include <boost/endian/conversion.hpp>
#include <iostream>
#include <cstddef>
int main() {
std::uint64_t foo = 1;
std::cout << boost::endian::endian_reverse(foo);
}
Output: 72057594037927936

Related

Why a[21] is not equal to a[021]? [duplicate]

This question already has answers here:
Why is initializing an integer in C++ to 010 different from initializing it to 10?
(4 answers)
Closed 1 year ago.
#include<bits/stdc++.h>
using namespace std;
int a[500];
int main()
{
a[21]=10;
if(a[21]==a[021])puts("Yes");
else puts("No");
return 0;
}
g++ -std=c++11
The output is No, can anyone tell me why?
In C++ a leading 0 on an integer literal means that value is in octal (similar to the way that 0x21 means that value is in hexadecimal).
Each of these values will be different. Here's a quick online demo:
http://cpp.sh/3cws4n
Note: the default output format for cout is decimal so the values you see are in decimal.

Is there an actual 4-bit integer data type in C++11 [duplicate]

This question already has answers here:
Is it possible to create a type in c++ that takes less than one byte of memory?
(5 answers)
Closed 5 years ago.
I require a 4 bit integer in a design for less memory use. In any version of c++ ,c++11 ,c++14 any can be used for the design.
There is no native 4bit datatype. But you could use an 8bit one to hold two 4bit values in the high/low nibble.
no, but you can use:
struct A {
unsigned int value1 : 4;
unsigned int value2 : 4;
};

What does this short C++ code mean? [duplicate]

This question already has answers here:
What does C++ struct syntax "a : b" mean
(5 answers)
":" (colon) in C struct - what does it mean? [duplicate]
(3 answers)
When to use bit-fields in C
(16 answers)
Closed 5 years ago.
This was a code snippet from a test question. The question was what the size of S would be.
struct S
{
char a : 4;
unsigned char b : 3;
signed char : 2;
char c : 1;
char d : 5;
};
What does the ":" do? Is there any difference when it is applied to a signed or unsigned char (or any other data type)? When is this usually used?
It's a bit field, it says that for example char a will only have 4 bits of memory instead of normal 8. Unsigned char b will have only 3 bits of memory. Number of bits limits the range of values it can hold.
Bit Field declares a class data member with explicit size, in bits. Adjacent bit field members may be packed to share and straddle the individual bytes.
http://en.cppreference.com/w/cpp/language/bit_field

struct size not as expected when using bit fields [duplicate]

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Padding in structures in C
(5 answers)
Closed 6 years ago.
With the below code:
struct S {
unsigned char b1 : 3;
int i;
};
I can deduce that char b1 is of 1 byte and using 3 bits of that and int is 4 bytes but when I try to print out the size of S, I get it 8 bytes instead of 5 bytes(1 for char+4 for int) as I expected and this is something I am not understanding by obvious.
Can something explain why size of S is 8 bytes?

Bitwise operators : why is ~35=-36? [duplicate]

This question already has answers here:
two's complement
(3 answers)
Closed 8 years ago.
so here is my code :
The aNSWER is -36
is -36 written as sign bit notation ?
i cant understand the bitwise conversion giong on, in using Dev c++
#include <iostream>
#include<conio.h>
using namespace std;
int main() {
printf("%d",~35);
getch();
}
Most significant bit determines if the number is negative or positive.