The bit representation of a variable in C++

print bits in c
bit fields in c examples
advantages of bit field in c
format specifier for binary in c
bit field in c pdf
c bit field union
bit field in c
c bit field array

I hope someone can help me out with this problem. I have this C++ program that will be able to print the bit representation of some variables.

#include <iostream>
#include <bitset>

//macro for extracting the ith bit from a given char.
#define EXTRACTBIT(ith,ch)(std::cout<<(ch & (1 << ith) ? 1 : 0))

/*printlnbits is a function which prints the bit representation of a variable (least 
significant bit must be shown on the right) followed by its size (in bits).*/

template <typename T> void printlnbits(T v)
{                                         
   const int v_size = sizeof(v) * 8;
   int j=0;
   char* ch = reinterpret_cast<char*>(&v);

   for (int n = v_size - 1; n >= 0; --n)
   {
       j++;
       EXTRACTBIT(n, *ch);
       if (j == 8)
       {
          std::cout << " ";
          j = 0;
       }
   }
   std::cout << "\t" << v_size << std::endl;
}
struct foo {
   int a = 2;
   char b = -1;
   unsigned long long int x = 1LLU << 63;
};

int main()
{
   const char a = 'a';
   const int b = -1L;
   const unsigned c = 1<<31;
   const float d = -0.0f;
   const long long unsigned e = 1LLU<<40;
   const foo f;
   printlnbits(a);
   printlnbits(b);
   printlnbits(c);
   printlnbits(d); 
   printlnbits(e);
   printlnbits(f); 
   return 0;
}

Code output:

The problem is with d, e and f variables. The bit representation of them is incorrect. I don't know what's wrong with my code. I'm a bit new in C++. Can anyone help????


You never increment pointer ch to the byte being inspected. So you inspect the same byte over and over again. At the same time n which is a global bit index is used instead of j.

#include <iostream>
#include <memory>
#include <climits>

template <typename T> void
printlnbits(T const & v)
{                                   
   auto const p_bytes_begin{reinterpret_cast<unsigned char const *>(::std::addressof(v))};
   auto const p_bytes_end{p_bytes_begin + sizeof(v)};
   auto p_byte{p_bytes_begin};
   auto bit_index_in_byte{0};
   for(;;)
   {
       ::std::cout << (((*p_byte) bitand (1 << bit_index_in_byte)) ? '1' : '0');
       ++bit_index_in_byte;
       if(CHAR_BIT == bit_index_in_byte)
       {
          ::std::cout << " ";
          bit_index_in_byte = 0;
          ++p_byte;
          if(p_bytes_end == p_byte)
          {
              break; // for(;;)
          }
       }
   }
   ::std::cout << "    " << (sizeof(v) * CHAR_BIT) << " bits" << ::std::endl;
}

online compiler

10000110     8 bits
11111111 11111111 11111111 11111111     32 bits
00000000 00000000 00000000 00000001     32 bits
00000000 00000000 00000000 00000001     32 bits
00000000 00000000 00000000 00000000 00000000 10000000 00000000 00000000     64 bits
01000000 00000000 00000000 00000000 11111111 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001     128 bits

Binary representation of a given number, C Program for the binary. // representation of a given number. void bin(unsigned n). {. /* step 1 */. if (n > 1). bin(n/2);. /* step 2 */. printf ( "%d" , n % 2);. } int main(  Bit Fields in C In C, we can specify size (in bits) of structure and union members. The idea is to use memory efficiently when we know that the value of a field or group of fields will never exceed a limit or is withing a small range.


I do not see increment of 'ch' here

  for (int n = v_size - 1; n >= 0; --n)
   {
       j++;
       EXTRACTBIT(n, *ch);
       if (j == 8)
       {
          std::cout << " ";
          j = 0;
       }
   }

You just see first byte.

Bit Fields in C, In C, we can specify size (in bits) of structure and union members. The above representation of 'date' takes 12 bytes on a compiler where an of the fields goes beyond the bits allocated to the variable and something interesting can happen. The type may be int, signed int, or unsigned int. 2. member_name. The name of the bit-field. 3. width. The number of bits in the bit-field. The width must be less than or equal to the bit width of the specified type. The variables defined with a predefined width are called bit fields.


Use this:

template<typename T>
void print(T v) {
    int s = sizeof(T) * 8;
    for (int i = s - 1; i >= 0; i--) {
        if ((i+1) % 8 == 0) std::cout << ' ';
        std::cout << ((v & 1 << i)? 1 : 0);
    }
    std::cout << '\n';
}

For float type and others, you can simply use:

std::cout << std::bitset<int(sizeof(float) *8 )>(d) << '\n';

C - Bit Fields, The above structure requires 4 bytes of memory space for status variable, but only 2 bits will be used to store the values. If you will use up to 32 variables each one  The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise OR operator is denoted by |. 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ________ 00011101 = 29 (In decimal)


[PDF] Lecture 03, An integer is typically represented by 4 bytes (or 32-bits). However this is given back. In addition to automatic storage class, C variables can be declared to be. In the C programming language, operations can be performed on a bit level using bitwise operators . Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR and NOT operators. Instead of performing on individual bits, byte-level operators perform on strings of eight bits (known as bytes) at a time.


C Programming Course Notes - Data Types, The "type" of a particular variable or constant determines how many bits are used used Unsigned integers use one bit pattern ( all 0s ) to represent zero and all  To print binary representation of unsigned integer, start from 31th bit, check whether 31th bit is ON or OFF, if it is ON print “1” else print “0”. Now check whether 30th bit is ON or OFF, if it is ON print “1” else print “0”, do this for all bits from 31 to 0, finally we will get binary representation of number.


Basics of Binary for C Programming, Term, C Variable, Bits, Value Range Unsigned, Value Range Signed then total them up, you get the decimal representation of binary 00101011, which is 43. C++ variables are stored internally as so-called binary numbers. Binary numbers are stored as a sequence of 1 and 0 values known as bits. Most of the time, you don’t really need to deal with numbers at the bit level; however, there are occasions when doing so is convenient. C++ provides a set of operators for this purpose.