You are here:

- Home
- Computing/Technology
- C/C++
- C++
- Data Types

Advertisement

QUESTION: Hi, I'm having some trouble understanding some data types and operators in C++, can u help answer these questions? Thanks.

1) What is the advantage of enumeration? Why using it when I can just use integer to replace it?

2) What does this (status & 8) do? If(status & 8) cout << “bit 4 is on”;

And this function:

Void show_binary (unsigned int u) { // why is it unsigned int?

Int t;

For (t = 128; t >0; t = t / 2) // how can this display the binary numbers

If (u & t) cout << “1”;

Else cout << “0”

Cout << “\n”; }

3) Why is the value of this expression 1010 instead of 1110?

J = 10;

I = (j++, j+100, 999+j);

4) I’m not exactly sure how this program works, can you explain?

#include <iostream>

Using namespace std;

Unsigned char rrotate (unsigned char val, int n);

Unsigned char lrotate (unsigned char val, int n);

Void show_binary (unsigned int u);

Int main() {

Char ch = ‘T’;

Cout << “Original value in binary: \n”;

Show_binary (ch);

Cout << “Rotating right 8 times: \n”;

For (int I = 0; I < 8; I++) {

Ch = rrotate (ch, 1);

Show_binary (ch); }

Cout << “Rotating left 8 times: \n”;

For (int I = 0; I < 8; I++ ) {

Ch = lrotate (ch, 1);

Show_binary (ch); }

Return 0; }

Unsigned char lrotate (unsigned char val, int n) {

Unsigned int t;

T = val;

For (int I = 0; I < n; I++) {

T = t << 1;

If (t & 256) t = t | 1; } // what does this do?

Return t; }

Unsigned char rrotate (unsigned char val. Int n) {

Unsigned int t;

T = val;

T = t << 8;

For (int I = 0; I < n; I ++) {

T = t >> 1;

If (t & 128) t = t | 32768; } // why is this different from that of the lrotate?

T = t >> 8; // why is this needed?

Return t; }

Void show_binary (unsigned int u) { // how can t converted to u without using casts?

Int t;

For (t = 128; t >0; t = t / 2)

If (u & t) cout << “1”;

Else cout << “0”

Cout << “\n”; }

ANSWER: 1) What is the advantage of enumeration? Why using it when I can just use integer to replace it?

An enumerated type is used to express the idea that a variable will be used for a specific purpose and should only be able to have a small limited number of values - for instance, a variable that stores the current direction only needs to store four values corresponding to north, south, east, and west. We can use an enumerated type for this:

enum direction_type { NORTH, EAST, SOUTH, WEST } ;

direction_type is a type, you can only assign one of NORTH, EAST, SOUTH, WEST to a variable of this type (without a cast). enums are thus less error prone (more typesafe).

enums also make your code much more readable and understandable. compare

if( mode == 5 ) with

if( mode == READ_ONLY )

3) Why is the value of this expression 1010 instead of 1110?

J = 10;

I = (j++, j+100, 999+j);

A pair of expressions separated by a comma is evaluated left-to-right and the value of the left expression is discarded. All side effects of the left expression, except for the destruction of temporaries, are performed before the evaluation of the right expression.

So, (j++, j+100, 999+j) is evaluated as

1. j++ - increment j, j is now 11

2. j+100 - yields 11+100 or 111, discarded

3. 999+j - yields 999+11 ie. 1010, this is the result of the expression.

The other questions relate to the use of bitwise operators in C++. For answers, see:

http://www.cprogramming.com/tutorial/bitwise_operators.html

http://www.java2s.com/Tutorial/C/0100__Operator/0140__Bitwise-Operator.htm

http://compsci.ca/v3/viewtopic.php?t=9893

---------- FOLLOW-UP ----------

QUESTION: I would like to add that in question 3, what i'm asking is why j + 100 is discarded when 999 + j is remained, please explain.

And in question 2 and 4, I can not find the answers in the web links that are provided, would you mind explaining them in texts? thanks

why j + 100 is discarded when 999 + j is remained,

The language specification states "expressions separated by a comma is evaluated left-to-right and the value of the left expression is discarded".

As j+100 is on the left side, its result is discarded.

999+j is on the right side, its result is the result of the expression.

2) What does this (status & 8) do?

In binary representation, 8 is 00001000. the '&' is the bit-wise and operator. status & 8 gives non-zero if bit 4 is set, zero otherwise.

4)

If (t & 256) t = t | 1; } // what does this do?

In binary representation, 256 is 10000000. the '&' is the bit-wise and operator, if( t & 256 ) is true if the most significant bit (bit eight) is set. the '|' is the bit-wise or operator, t = t | 1 sets the least significant bit in t.

The idea is that when you rotate the bits, to make the most significant bit should be moved to the least significant bit position. t = t << 1; rotates left by one bit, losing the most significant bit and moving zeroes into the least significant bit position. The above code corrects it by 'if the most significant bit is set, set the least significant bit; else leave it as zero.

rrotate rotates right. This is done by working on 16 bits.

First, t = t << 8; shifts the 8 bit value left by 8 bits to get a 16 bit value in which the original 8 bits are at the left and the 8 bits at the right are zeroes.

if( t & 128 ) t = t | 32768; Reasons similar to lrotate( 32768 has bit sixteen set and all other bits zero).

Finally, t = t >> 8; shifts it right by 8 bits to put the result in the 8 bits at right.

NOTE: The code is not portable, it makes several assumptions about the representation of integral data types on the target architecture - for instance, that a char consists of precisely eight bits.

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

about 15 years or so**Education/Credentials**

post graduate engineer