Home c Unswied __int64 translation to C++ bits

Unswied __int64 translation to C++ bits

Author

Date

Category

There is an unsigned __ int64 Is there a way in C / C++ get the bits line from it?


Answer 1, Authority 100%

For example, like this:

unsigned __int64 num = 100;
Char BUF [64];
_itoa_s (num, buf, 2);
COUT & LT; & LT; buf;

or even so (slightly pervert):

unsigned __int64 num = 100;
STD :: STRING ST = "";
While (NUM)
{
  ST .INSERT (0, NUM & AMP; 1? "1": "0");
  Num & gt; & gt; = 1;
}
COUT & LT; & LT; st.c_str () & lt; & lt; Endl;

Answer 2, Authority 150%

C++: Bitset

C: iToa ()


Answer 3, Authority 150%

If you really have a simple pragmatic task of installing, checking and resetting a bit by its number and 64 bits you enough, you can use simple macros.

Here is an example

# include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; stdint.h & gt;
#Define Setbit (U, N) ((U) | = ((int64_t) 1 & lt; & lt; (n)))
#Define CLRBIT (U, N) ((U) & amp; = ~ ((int64_t) 1 & lt; & lt; (n)))
#Define TSTBIT (U, N) ((U) & amp; ((int64_t) 1 & lt; & lt; (n)))
int.
Main ()
{
 uint64_t x = 0;
 Setbit (x, 0);
 Setbit (x, 1);
 Setbit (x, 62);
 Setbit (x, 63);
 PrintF ("% LLX \ N", (Long Long) X);
 CLRBIT (X, 1);
 CLRBIT (X, 62);
 PrintF ("% LLX \ N", (Long Long) X);
 If (! Tstbit (X, 62))
  PrintF ("Bit 62 IS Cleared \ N");
 if (TSTBIT (X, 0))
  Printf ("Bit 0 is set \ n");
 Setbit (x, 30 + 2);
 PrintF ("% LLX \ N", (Long Long) X);
 RETURN PUTS ("END") == EOF;
}

works in C and in C++.

avp @ avp-ubu1: ~ / hashcode $ g ++ bits.c
AVP @ AVP-UBU1: ~ / Hashcode $ ./a.out
C00000000000033.
8000000000000001.
Bit 62 is Cleared
Bit 0 is set
8000000100000001.
End.
AVP @ AVP-UBU1: ~ / Hashcode

Naturally, in a similar way (taking or constructing the correct mask), you can install, reset or check the bit group for one operation.


Answer 4, Authority 50%

Why no one has not published a solution in Compile-Time with Template Metaprogramming?

template & lt; int n, TypeName S & GT;
 Struct to_binary_string_Rec
 {
   TypeDef TypeName To_Binary_String_REC & LT;
       N / 2,
       TypeName BOOST :: MPL :: PUSH_FRONT & LT;
           s,
           Boost :: MPL :: CHAR_ & LT; (N & AMP; 1)? '1': '0' & gt;
       & GT; :: Type
   & gt; :: Type Type
 };
 TEMPLATE & LT; TYPENAME S & GT;
 struct to_binary_string_rec & lt; 0, s & gt;
 {
   TypeDef TypeName Boost :: MPL :: if_ & lt;
       TypeName Boost :: MPL :: EMPTY & LT; S & GT; :: Type,
       Boost :: MPL :: STRING & LT; '0' & GT;
       S.
   & gt; :: Type Type;
 };
 TEMPLATE & LT; INT N & GT;
 Struct to_binary_string
 {
   TypeDef TypeName To_Binary_String_REC & LT; N, Boost :: MPL :: String & Lt; & gt; & gt; :: Type Type;
 };
 #define to_binary_string (n) Boost :: MPL :: C_STR & LT; to_binary_string & lt; N & GT; :: Type & GT; :: Value

(Sorry, did not test, because there is no boost at hand.)


Answer 5, Authority 50%

No % B Analog % x , % o printf-specimens , no STD :: BIN Analog STD :: HEX, STD :: OCT IOMANIP for binary system and no function _itoa_s () in standard C / C++.

you can use BitSet & lt; & gt; to unsigned integer in “01 “Strike with bits to turn:

# include & lt; bitset & gt;
#Include & lt; iostream & gt;
#Include & lt; Limits & gt;
INT MAIN ()
{
  uint64_t n = 123;
  STD :: BITSET & LT; STD :: NUMERIC_LIMITS & LT; DECLTYPE (N) & GT; :: Digits & GT; Bits {N};
  STD :: COUT & LT; & LT; bits.to_string ();
}

Example:

$ C++ -std = C++ 11 * .cc & amp; & amp; ./a.out.
000000000000000000000000000000000000000000000000000111011

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions