How to isolate a nibble from a 16bit binary number

Sep 26, 2010 at 8:00pm
Hey all-

I am a VERY beginner C++ programmer. A homework problem has me returning the shelf that a certain product is supposed to be on in a store. The shelf number is the first 4 bits of a 16 bit binary number. How do I isolate just the first 4 bits?

The "hint" was to use bitwise functions (I've seen bitmasking on the web), but I'm not sure how to do it.

To complicate things, the user enters the 16bit binary number in base ten. I think I can figure out how to convert that on my own.
Sep 26, 2010 at 8:09pm
We can't really give any more hints other than the one given...try some examples of using bitwise &, |, etc on some binary numbers and look at the output, then see what you get.
Last edited on Sep 26, 2010 at 8:09pm
Sep 26, 2010 at 8:14pm
I get the bitmask and bitwise functions. My problem is that if I mask the number so that I have, for example, 1111000000000000, I'm going to get a reeally big number (because its 1x2^14 or whatever). So how do I just get the value of the four leftmost digits after I mask them?
Sep 26, 2010 at 8:15pm
Use the right-shift operator. (Do it first, then mask.)

Hope this helps.
Sep 26, 2010 at 8:34pm
Ok, completely understood. Now my only problem is converting the decimal number to binary. I saw this example:

int a = 17;
cout << bitset<16>( a ) << endl;

But this just outputs the binary number to the screen. How do I use the number in further code?

Would this work (for example)?

int a = 17;
return bitset<16>( a );
Sep 26, 2010 at 8:51pm
You don't have to convert your number to binary format in order to operate on it using bitwise operators.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <bitset>
using namespace std;

int main ()
{
    int a, b;

    a=4;
    cout << "a: " << bitset<8>(a) << endl;

    b=a>>1;
    cout << "b: " << bitset<8>(b) << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}
Sep 26, 2010 at 8:55pm
You know that 'int ' is made out of (usually 4) bytes. You also know that a byte is made of 8 bits. You know that bit can be only 1 or 0. You could assume (or know) that bit means Binary digIT. So how can these 32 binary digits make a decimal number?
Answer: they can't! All data that is in your computer is in binary. At compile time 17 is converted into 00010001. cin and cout also do conversion from strings of decimal digits to binary values (or vice versa).
Last edited on Sep 26, 2010 at 8:57pm
Sep 26, 2010 at 9:11pm
So how can these 32 binary digits make a decimal number?
PROTIP: numbers don't have bases.
Sep 26, 2010 at 9:39pm
@hamsterman: Yes, I realize that everything is done with binary. That's fairly basic. But I din't realize I could do binary operations on a number in C++ without telling he computer I wanted to do binary stuff. Makes it much easier.

But anyway, here's my final program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//
//Toy.cpp

#include <iostream>
#include <bitset>
using namespace std;

int main()
{
int x;
int y=15;
int a;

cout << "Enter a 16-digit binary number (specified as a decimal number): \n";
cin >> x;
//cout << bitset<16>(x) << endl;

x=x>>12;
//cout << bitset<16>(x) << endl;

cout << "Your toy should be on shelf " << x << endl;

return (0);
}


Not sure why I made variable a, probably just left over.
Topic archived. No new replies allowed.