Bits in C++ on the example code.

People help out there is nothing in the Internet about bits on C++, or else they are called bit fields.

The code is there, but I didn't understand everything.

Tell us what's what, please. Development environment C++ Builder 2010, if necessary.

#include<iostream>
#include <conio.h>
using namespace std;

#include<iomanip>

using std::setw;
using std::cin;

void bit(int);

int main()
{
int x;

cout<<"vvedite сeloe x: ";
cin>>x;

bit(x);
getch();

return 0;
}
void bit(int x)
{
const int S=8*sizeof(int)-1;
const int M=1<<S;

cout<<setw(7)<<x<<" = ";

for(int i=1;i<=S+1;i++){
cout<<(x&M ? '1':'0');
x<<=1;

if(i%8==0)
cout<<' ';
}
cout<<endl;
}

//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
const int S=8*sizeof(int)-1;

This type of int includes 4 bytes, each byte contains 8 bits so we multiply by 8, and why do we subtract 1? (after all, it turns out 31 or the numbering goes from 0)

//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
const int M=1<<S;

Shifts the bits of the first operand to the left by the number of bits specified by the second the operand.When performing a shift operation, the right-hand free bits are filled with 0. This is not understood at all

//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
for(int i=1;i<=S+1;i++)
{
cout<<(x&M ? '1':'0');
x<<=1;

if(i%8==0)
cout<<' ';}

What does this do (mb only that the loop runs through 32 characters) but for what. Well, in general, this fragment is generally stupid. I don't understand help me understand.

Author: Stanislav Pankevich, 2012-12-10

5 answers

The left shift operation is, roughly speaking, multiplication by 2. The fact is that the computer represents numbers in binary (where there are only two digits - 0 and 1). Let me explain with an example. Let's say you have a 1-byte variable and it stores the number 10. Appear. In binary code, it will look like this: 00001010. To understand what this means, you need to know a little about number systems. In short, these zeros and ones mean that your number has the following form: 0 * 128 + 0 * 64 + 0 * 32 + 0 * 16 + 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = 0 + 0 + 0 + 0 + 8 + 0 + 2 + 0 = 10 If we apply the left shift operation to this number by n positions, then all its zeros and ones will move n steps to the left, and we will get this number: 00010100, that is 0 + 0 + 0 + 16 + 0 + 4 +0 + 0 = 20. That is, by shifting to the left by one digit, we multiply the number by 2. Accordingly, if we shift not by one digit, but by two, three, four, and so on, then this will be a multiplication by 2 in the second, third, fourth, and so on degrees. Similarly, for a right shift, which is an integer division by 2 to the power of n, where n is the number of digits by which the shift occurs. Shift operations are much faster in terms of performance than the corresponding multiplication and even more so division. Therefore, many compilers replace multiplication and division with a corresponding shift whenever possible.

Now a few words about the expression cout

if(x & M)
    cout << 1;
else
    cout << 0;

Here, the question mark is preceded by the condition (x&M), followed by the question mark - the result of the expression if the condition is true, and after the colon - the result if the condition is incorrect. P.S. By the way, > after cin and cout are not shift operators

 4
Author: DreamChild, 2012-12-10 17:28:32

Bit fields are special fields in structures that take up a certain number of bits, so that multiple fields can take up one byte (or more than one). It was born out of the need to conveniently describe working with hardware registers. Primarily I / O (it seems).

   struct myStr {
     int a:1;
     int b:2;
   };

Field a takes one bit, field b takes two;

Or maybe just to save memory - it was not enough then. The analogue of this miracle in pascal is packed record (if sclerosis does not change me)

 4
Author: alexlz, 2012-12-11 11:15:14

@Atom, answering questions:

1. const int S=8*sizeof(int)-1; - let's find out how many bits are needed for an integer without a signed type, right?

No. The number of bits in int is S+1, and S is the maximum bit number in int.

2. const int M=1<<S; - shifted 32 bits to the left by 1 bit, if so, why do we shift the bits, why don't we leave them in place, how do I understand this?

Yes, we shift all the bits to the left, bit 0 becomes zero. For example

x: 10010000 00001000 10101111 100000001 после x<<=1 превратится в
   00100000 00010001 01011111 000000010

Specifically for our example with S and M

S=8*sizeof(int)-1; // S = 31
M=1<<S;  
1:     00000000 00000000 00000000 00000001
1<<31: 10000000 00000000 00000000 00000000

Why don't we leave the bits in place? Very simple, we don't need it.

What do we really want? We want to find out the value of the bit of an integer at the position n (for example, 2). To do this, we want to use the AND bit operation (operation & in C / C++).

v = 266  :    00000000 00000000 00000001 00001010 // видно, что бит 2 это 0
1        :    00000000 00000000 00000000 00000001 // число 1, будем делать 1 << 2
M = 1<<2 :    00000000 00000000 00000000 00000100 // сдвинули
v & M    :    00000000 00000000 00000000 00000000 // число 0 т.е. бит 2 был 0

3. x<<=1 if(x&M) { cout<<1;} else {cout<<0;}

      for (int i = 0, x = 1; i < sizeof(v)*8; i++, x <<= 1)
          if (v & x)
              cout << "bit "<<i<<" is set\n";

This prints which bits of the number v are set (to 1). Step by step:

x = 1,  i = 0, v = 266
v:    00000000 00000000 00000001 00001010
x:    00000000 00000000 00000000 00000001
v&x:  00000000 00000000 00000000 00000000 // ничего не печатаем
x << 1, i = 1, v = 266
v:    00000000 00000000 00000001 00001010
x:    00000000 00000000 00000000 00000010
v&x:  00000000 00000000 00000000 00000010 // cout << "bit 1 is set"
x << 1, i = 2, v = 266 
v:    00000000 00000000 00000001 00001010
x:    00000000 00000000 00000000 00000100
v&x:  00000000 00000000 00000000 00000000 // ничего не печатаем
x << 1, i = 3, v = 266
v:    00000000 00000000 00000001 00001010
x:    00000000 00000000 00000000 00001000
v&x:  00000000 00000000 00000000 00001000 // cout << "bit 3 is set"

, etc. All 32 steps should not be described will. I hope it's clear now. Anything else you want to clarify - ask.

 2
Author: avp, 2012-12-15 16:33:16

I would also recommend that you use a special class from the STL library, which comes with almost any C++ package (whether it's MinGW, Borland C++ Builder, or a Microsoft implementation).
This class is called bitset, to use it, you just need to connect the corresponding header file , like this:

#include <bitset>

Unfortunately, it doesn't have a lot of features (it's more of a wrapper than a full-featured class for working with bits in the variable), but nevertheless, you will at least know that such a class exists.

 1
Author: PaulD, 2012-12-11 20:36:31
 1
Author: STIZZ, 2012-12-13 20:52:01