Couple Questions!

Pages: 12
oh ok I got it, I already know about the bitwise operators but I never really heard of bitmask, only bits.


I have a small question about line 16 though, why is it that after the numeric_limits<unsigned> you put digits-1u?

Is digits a static member of numeric_limits? If so what does it mean?
Last edited on
Also how does this

1
2
std::ostream& operator<<(std::ostream& os, Binary b)
{


Overload the operator, is ostream basiclly cout, or does cout return ostream that the operator overload gets?

Whats exactly going on here?
@Anmol444

http://en.cppreference.com/w/cpp/types/numeric_limits/digits

AFAIK the members are static.
Are the bitwise operators very useful or is just there if anyone prefers it, cause I cant really visualize there effects so its hard for me to figure out whats going on.

Can you please explain the code that cire posted.

Also what does base radix mean?
Oh ok I got it, the radix is 2 cause there is only 0 and 1. Thanks!
Are the bitwise operators very useful or is just there if anyone prefers it, cause I cant really visualize there effects so its hard for me to figure out whats going on.


The most common use for bitmasks and bitwise operators that I've seen (*) is as an easy way to pack several boolean flags into one variable.

Consider a char. Typically, a char will have 8 bits. If set to 0 (or '\0' if you prefer), then the bits are:

00000000

If set to 2, then:

00000010

If set to 7:

00000111

etc.

If, for some reason, you needed to use up to 8 boolean flags for something, then you can use each of those bits to store a single flag, so that you can use a single char to store those 8 flags. You can then use bitmasks and bitwise operators to set and read the values of each flag independently.

This is known as using "bit flags", and I imagine a Google search for "C++ bit flags" will get you more information on how to use them. Often, you'll find that third-party software uses them in their interface.

Edit: Other than that, you find them used in things like interfaces to hardware, communication protocols, and other low-level areas where it's helpful to pack as much data into the smallest amount of memory as possible.


(*) Other peoples' experiences may differ from mine, of course.
Last edited on
Ok thanks!
I have a small question about line 16 though, why is it that after the numeric_limits<unsigned> you put digits-1u?


To understand that you need to look up the bitshift operator (<<) and look up numeric_limits.

I'm beginning to think you don't experiment on your own and don't bother making a serious effort to research matters for yourself prior to asking questions here, at least for any subjects that are tangent to your original line of questioning in a thread.

If you have this small question and I exhorted you to play around with the code prior to asking more questions, why didn't you change that to see what happened?

Armed with the knowledge you might've researched for yourself, you might've changed the code to look like this (and mimic the code in operator<<) in order to satisfy your curiosity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <limits>
#include <iostream>

class Binary
{
public:
    Binary(unsigned value) : _value(value) {}
    operator unsigned() { return _value ; }

private:
    unsigned _value ;
};

std::ostream& operator<<(std::ostream& os, Binary b)
{
    unsigned bitmask = 1u << (std::numeric_limits<unsigned>::digits-1u) ;

    while ( bitmask )
    {
        os << ((b & bitmask) != 0) ;
        bitmask >>= 1 ;
    }
    return os ;
}

int main()
{
    const unsigned digits = std::numeric_limits<unsigned>::digits ;

    std::cout << "digits for unsigned: " << digits << '\n' ;
    std::cout << Binary(1 << digits) << ": " << (1<<digits) << '\n' ;
 
    unsigned bitmask = 1u << digits-1 ;

    while ( bitmask )
    {
        std::cout << Binary(bitmask) << ": " << bitmask << '\n' ;
        bitmask >>= 1 ;
    }
}


Part of programming is problem solving. Practice solving problems.
I did look it up and figured out what it did and I knew what the bitshift operator did, I just have trouble visualizing the bitwise operators effects.

I guess I will try doing a bit more research before I come ask questions here, and yea I do put the effort to research it before I ask the question here, I know it seems rude if I just ask here without any effort of my own.

But anyways thanks!
Last edited on
Topic archived. No new replies allowed.
Pages: 12