Discussion:
Is std::bin standard? [C++]
(too old to reply)
wwwolf
2005-04-03 01:28:18 UTC
Permalink
I'm trying to make a number conversion program but can't get it to compile
because it states that bin is undeclared. std::oct and std::hex are not
producing any compilation errors. If std::bin is not standard, then how do I
convert varaibles to binary code?

Oh, and if you see any novice mistakes then please tell me. I will probably make
this into a function later ( for my next self-assignment ) but I wanted to get
it working first.

Thanx for your help.

::::::::::::::::::::::::::::::::::::::::::
// My first number conversion program
#include <iostream>

int main()
{
// using statements in ABC order
using std::bin;
using std::cin;
using std::cout;
using std::flush;
using std::hex;
using std::oct;

long num;
int choice;

while(1)
{
cout << "\nEnter a decimal number ( 0 to quit ): " << flush;
cin >> num;
{
if ( num == 0 )
break;
else
{
cout << " Select a conversion choice...(1)bin, (2)oct, (3)hex : " <<
flush;
cin >> choice;
{
if ( choice == 1 )
cout << bin << num << flush;
else if ( choice == 2 )
cout << oct << "0" << num << flush;
else if ( choice == 3 )
cout << hex << "0x" << num << flush;
else if ( choice > 3 )
cout << "\n You don't follow directions very well do you?" <<
flush;
}
}
}
}

return 0;
}
::::::::::::::::::::::::::::::::::::
osmium
2005-04-03 02:16:26 UTC
Permalink
Post by wwwolf
I'm trying to make a number conversion program but can't get it to compile
because it states that bin is undeclared. std::oct and std::hex are not
producing any compilation errors. If std::bin is not standard, then how do I
convert varaibles to binary code?
Oh, and if you see any novice mistakes then please tell me. I will probably make
this into a function later ( for my next self-assignment ) but I wanted to get
it working first.
Thanx for your help.
// My first number conversion program
#include <iostream>
int main()
{
// using statements in ABC order
using std::bin;
using std::cin;
using std::cout;
using std::flush;
using std::hex;
using std::oct;
long num;
int choice;
while(1)
{
cout << "\nEnter a decimal number ( 0 to quit ): " << flush;
cin >> num;
The statement above converted from character code to binary. The >> and <<
operators are trained to convert as well as display or store. num *is*
binary, what the human typed was in a character code, usually ASCII.

<snip.
Chris ( Val )
2005-04-03 05:19:44 UTC
Permalink
"wwwolf" <***@alltel.net> wrote in message news:c89b5$424f4621$a6664384$***@ALLTEL.NET...
| I'm trying to make a number conversion program but can't get it to compile
| because it states that bin is undeclared. std::oct and std::hex are not
| producing any compilation errors. If std::bin is not standard, then how do I
| convert varaibles to binary code?
|
| Oh, and if you see any novice mistakes then please tell me. I will probably make
| this into a function later ( for my next self-assignment ) but I wanted to get
| it working first.
|
| Thanx for your help.
|
| ::::::::::::::::::::::::::::::::::::::::::
| // My first number conversion program
| #include <iostream>
|
| int main()
| {
| // using statements in ABC order
| using std::bin;

[snip]

Well, it's no good guessing :-)

Seriously, to answer your question, no, there is no such
manipulator as 'std::bin' in C++, however, if you read on,
you will se that all is not lost and that there are ways to
obtain a binary representation as desired.

The first way is to use some good 'ol' biteset operations
such as the bitwise operators: '&', '|', '^', '~', '>>' and
'<<'.

For example, here I am using the bitwise 'AND' operator to
check is a particular bit of the numeric value is set, and
if it is, I output the message "Bit 'X' is SET".

The hexadecimal values represent the bit position for Num:

unsigned int Num( 26 );

if( Num & 0x01 ) // -- LSB --
std::cout << "Bit 1 is SET" << std::endl;
if( Num & 0x02 )
std::cout << "Bit 2 is SET" << std::endl;
if( Num & 0x04 )
std::cout << "Bit 3 is SET" << std::endl;
if( Num & 0x08 )
std::cout << "Bit 4 is SET" << std::endl;
if( Num & 0x10 )
std::cout << "Bit 5 is SET" << std::endl;
if( Num & 0x20 )
std::cout << "Bit 6 is SET" << std::endl;
if( Num & 0x40 )
std::cout << "Bit 7 is SET" << std::endl;
if( Num & 0x80 ) // -- MSB --
std::cout << "Bit 8 is SET" << std::endl;

In this particular case (if we have done everything right), we
should see the output...

Bit 2 is SET
Bit 4 is SET
Bit 5 is SET

...for the representation of the number '26'.

Ok, fine, but that is too difficult I hear you say ?

Well yes, of course it is, and you wouldn't normally
want to write code in such a boring and erroneous
way, therefore enter loops, yep a loop:

for( unsigned Bit = 0x80; Bit >= 0x01; Bit >>= 1 )
( Num & Bit ) ? std::cout << '1' : std::cout << '0';

Ok, now that is much better.

Can we improve it ?
Sure we can, enter functions :-)

std::string DisplayBits( unsigned Num )
{
std::string BitBuffer;
for( unsigned Bit( 0x80 ); Bit >= 0x01; Bit >>= 1 )
( Num & Bit ) ? BitBuffer.push_back( '1' ) :
BitBuffer.push_back( '0' );

return BitBuffer;
}

Wow, now we can just do this:
std::cout << DisplayBits( Num ) << std::endl;

...to obtain a representation of our number in bits.

But wait, there's more <G>

Now that you know how to do it the hard way, here is an
easier way that we can deal with bitsets in C++:

C++ offers a template class known as: 'std::bitset<N>()',
and it can handle all the intricate details for us. All
we have to do to use it, is include the appropriate header:

# include <bitset>

...and then just use a temporary 'std::bitset<N>()' object
as shown below:

std::cout << std::bitset<16>( Num ) << std::endl;

For completeness, here is a small program that demonstrates
all of the above:

# include <iostream>
# include <ostream>
# include <bitset>

std::string DisplayBits( unsigned Num )
{
std::string BitBuffer;
for( unsigned Bit( 0x80 ); Bit >= 0x01; Bit >>= 1 )
( Num & Bit ) ? BitBuffer.push_back( '1' ) :
BitBuffer.push_back( '0' );

return BitBuffer;
}

int main()
{
unsigned int Num( 26 );
std::cout << "Num: " << Num << '\n' << std::endl;

if( Num & 0x01 ) // -- LSB --
std::cout << "Bit 1 is SET" << std::endl;
if( Num & 0x02 )
std::cout << "Bit 2 is SET" << std::endl;
if( Num & 0x04 )
std::cout << "Bit 3 is SET" << std::endl;
if( Num & 0x08 )
std::cout << "Bit 4 is SET" << std::endl;
if( Num & 0x10 )
std::cout << "Bit 5 is SET" << std::endl;
if( Num & 0x20 )
std::cout << "Bit 6 is SET" << std::endl;
if( Num & 0x40 )
std::cout << "Bit 7 is SET" << std::endl;
if( Num & 0x80 ) // -- MSB --
std::cout << "Bit 8 is SET" << std::endl;

std::cout << "\nNum as bits: " << DisplayBits( Num )
<< std::endl;

std::cout << "Num as bits: " << std::bitset<16>( Num )
<< std::endl;

return 0;
}

Hope this helps,
Chris Val
Chris ( Val )
2005-04-03 05:31:22 UTC
Permalink
"Chris ( Val )" <***@bigpond.com.au> wrote in message news:Q1L3e.22143$***@news-server.bigpond.net.au...
|
| "wwwolf" <***@alltel.net> wrote in message news:c89b5$424f4621$a6664384$***@ALLTEL.NET...
|| I'm trying to make a number conversion program but can't get it to compile
|| because it states that bin is undeclared. std::oct and std::hex are not
|| producing any compilation errors. If std::bin is not standard, then how do I
|| convert varaibles to binary code?
||
|| Oh, and if you see any novice mistakes then please tell me. I will probably make
|| this into a function later ( for my next self-assignment ) but I wanted to get
|| it working first.
||
|| Thanx for your help.
||
|| ::::::::::::::::::::::::::::::::::::::::::
|| // My first number conversion program
|| #include <iostream>
||
|| int main()
|| {
|| // using statements in ABC order
|| using std::bin;
|
| [snip]
|
| Well, it's no good guessing :-)
|
| Seriously, to answer your question, no, there is no such
| manipulator as 'std::bin' in C++, however, if you read on,
| you will se that all is not lost and that there are ways to
| obtain a binary representation as desired.
|
| The first way is to use some good 'ol' biteset operations
| such as the bitwise operators: '&', '|', '^', '~', '>>' and
| '<<'.

[snip]

Oh, I almost forgot.

In addition to my previous post, when working
with bitsets and char, also look up the following
macros for maximum portability:

CHAR_BIT Type char, number of bits
CHAR_MAX Type char, minimum value
CHAR_MIN Type char, maximum value

You will need to include the header:

<limits.h>, but prefer to use <climits> (without
the '.h'), if your compiler is modern enough to
support it.

The C++ Standard Library also offers such functionality
in: 'std::numeric_limits<T>()', though a little more
complex to understand.

Cheers,
Chris Val
wwwolf
2005-04-03 06:49:03 UTC
Permalink
Chris ( Val ) wrote:
<snip>
Post by Chris ( Val )
std::string DisplayBits( unsigned Num )
{
std::string BitBuffer;
for( unsigned Bit( 0x80 ); Bit >= 0x01; Bit >>= 1 )
<snip>
Post by Chris ( Val )
Hope this helps,
Chris Val
Thank You! It is nice to know the "start from scratch method" as well as the
"library header method". I don't know how I will ever discover all of these
jewels (functions) in the standard libs. :-(

I have tried by looking in the header files but they have very little comments
and what comments appear are usually over my head. Is there a good "function
list" resource describing what they do along with the header lib that they are
in?

I'm not sure I understand the syntax of ( ; ; Bit >>= 1 ) in the increment
decrement position of the for loop in the DisplayBits function. What does >>=
mean and what is this operator called?

Again, Thank You very much for your detailed explanation. It has been a big
help.
Chris ( Val )
2005-04-03 07:18:51 UTC
Permalink
"wwwolf" <***@alltel.net> wrote in message news:c2130$424f8e82$a666425b$***@ALLTEL.NET...
| Chris ( Val ) wrote:
| <snip>
| > std::string DisplayBits( unsigned Num )
| > {
| > std::string BitBuffer;
| > for( unsigned Bit( 0x80 ); Bit >= 0x01; Bit >>= 1 )
| <snip>
| > Hope this helps,
| > Chris Val
|
| Thank You! It is nice to know the "start from scratch method" as well as the
| "library header method". I don't know how I will ever discover all of these
| jewels (functions) in the standard libs. :-(
|
| I have tried by looking in the header files but they have very little comments
| and what comments appear are usually over my head. Is there a good "function
| list" resource describing what they do along with the header lib that they are
| in?

Not really, but you can find a lot of good reference information here:
http://www.dinkumware.com/manuals/reader.aspx?lib=cpp

Ultimately however, you need a really good book to help you get
through this stuff (if you don't already have one, of course).

It is getting on a little in age now, but it is still one of the
very best books around: http://www.josuttis.com/
The C++ Standard Library - "A Tutorial and Reference"
By: Nicolai M. Josuttis - ISBN 0-201-37926-0

| I'm not sure I understand the syntax of ( ; ; Bit >>= 1 ) in the increment
| decrement position of the for loop in the DisplayBits function. What does >>=
| mean and what is this operator called?

It called a "right shift" compound assignment operator.

It is responsible for shifting all bits by one position
to the right. For example, observer the following output:

unsigned int Num( 128 );
std::cout << Num << std::endl;

Num >>= 1;
std::cout << Num << std::endl;

Num >>= 1;
std::cout << Num << std::endl;

// Instead of the value '16', we now get the value '8'
// because we shifted by '2'
Num >>= 2;
std::cout << Num << std::endl;

| Again, Thank You very much for your detailed explanation. It has been a big
| help.

Any time.

Cheers,
Chris Val
Chris ( Val )
2005-04-03 06:25:18 UTC
Permalink
"wwwolf" <***@alltel.net> wrote in message news:c89b5$424f4621$a6664384$***@ALLTEL.NET...
| I'm trying to make a number conversion program but can't get it to compile
| because it states that bin is undeclared. std::oct and std::hex are not
| producing any compilation errors. If std::bin is not standard, then how do I
| convert varaibles to binary code?
|
| Oh, and if you see any novice mistakes then please tell me. I will probably make
| this into a function later ( for my next self-assignment ) but I wanted to get
| it working first.
|
| Thanx for your help.
|
| ::::::::::::::::::::::::::::::::::::::::::
| // My first number conversion program
| #include <iostream>
|
| int main()
| {
| // using statements in ABC order
| using std::bin;
| using std::cin;
| using std::cout;
| using std::flush;
| using std::hex;
| using std::oct;
|
| long num;
| int choice;

It is good practice to initialise your objects with a meaningful
value. Not such a big deal for this little project, but something
to keep in mind, because it can lead to trouble :-)

| while(1)
| {
| cout << "\nEnter a decimal number ( 0 to quit ): " << flush;
| cin >> num;

It is also good practice to check if the 'cin' object was successful
when reading in the input from the user. What happens if you enter
a character value, say 'C' instead of a number ?

if( !cin )
// Take action...

The most likely action you will take, is to re-ask the user
for another entry, after restoring the stream state. You
can do that with cin.clear(), and remove all the remaining
stream garbage with cin.ignore( N, '\n' ), where 'N' is the
number of characters to ignore.

Have a go at this, and if you need some help, then just post
back, and I or someone else will help out.

| {
| if ( num == 0 )

// You could use the following as an alternative:
if( !num )

| break;
| else
| {
| cout << " Select a conversion choice...(1)bin, (2)oct, (3)hex : " <<
| flush;
| cin >> choice;
| {
| if ( choice == 1 )
| cout << bin << num << flush;
| else if ( choice == 2 )
| cout << oct << "0" << num << flush;
| else if ( choice == 3 )
| cout << hex << "0x" << num << flush;
| else if ( choice > 3 )
| cout << "\n You don't follow directions very well do you?" <<

[snip]

Consider replacing the above with a 'switch' statement:

std::cin >> choice;
switch( choice )
{
case 1 :
cout << num << flush;
break;
case 2 :
cout << oct << "0" << num << flush;
break;
case 3 :
cout << hex << "0x" << num << flush;
break;
default:
cout << "\n You don't follow directions very well do you?" << flush;
}

Hope this helps,
Chris Val
Daniel Schüle
2005-04-03 15:45:19 UTC
Permalink
Post by wwwolf
int choice;
choice is an int => signed int
Post by wwwolf
while(1)
while(true) also possible

[...]
Post by wwwolf
cin >> choice;
{
if ( choice == 1 )
cout << bin << num << flush;
else if ( choice == 2 )
cout << oct << "0" << num << flush;
else if ( choice == 3 )
cout << hex << "0x" << num << flush;
else if ( choice > 3 )
cout << "\n You don't follow directions very well do you?" <<
flush;
}
{} are not necessary
what if one would enter -3 .. remember int is signed
switch(choice) is a better solution in this case
but to rewrite it in if/else syntax you should change it to

cin >> choice;
if ( choice == 1 )cout << bin << num << flush;
else if ( choice == 2 )cout << oct << "0" << num << flush;
else if ( choice == 3 )cout << hex << "0x" << num << flush;
else cout << "\n You don't follow directions very well do you?" << flush;

Regards

--Daniel

Continue reading on narkive:
Loading...