C++ :: Significance Of Assigning A Number Using Bitwise Operators

May 22, 2013

What is the difference between at performance level, if any, between the following cases, during assignment?

case 1: #define Value_16 16

and

case 2: #define Value_16 (1<<4)e.

View 2 Replies


ADVERTISEMENT

C/C++ :: Number Conversions Using Bitwise Operators

Feb 13, 2014

I have a project assignment for school to write a program that does number conversions using bitwise operators. The premise is that the user enters a number with one of three letter prefixes -- Q1232, O6322, H762FA, etc. -- and the program will take that number and convert it to the other two number bases. Q is for quarternary, O is for octal, and H is for hexadecimal. The transformations should be done using bitwise operators and bit shifting. I am guessing I need to scan the number, convert it to binary, then convert it to the other two bases.

However, I am completely new to bitwise operators and bit shifting, so how to convert numbers of different bases to binary and then binary to other bases using these bit and bitwise functions. I don't have much code done yet, since I am still unsure of how to approach it, but I'll post what little I have.

Here it is:

#include <stdio.h>
#include <string.h>
int main() {
char numType;
printf("
The user will enter a number up to 32 digits in quarternary
");
printf("(base 4), octal (base 8), or hexadecimal (base 16). If in
");

[Code] ....

I figure in each case I can write a function that converts the entered number to binary, then maybe two more functions that convert said binary number to the other bases. For default in the switch I will tell the user they entered an invalid number. I don't have the program looping until the user types 'EXIT' yet, but I will once I figure out anything about these bitwise operators.

View 2 Replies View Related

C :: Processor Handling Of Bitwise Operators

Mar 6, 2015

what order a CPU would process the following arithmetic problem: 5 - (-9) = 14? Would the CPU recognize that the 'minus a minus' combination simply represents 5 + 9, and proceed with that addition, or would the CPU have to first calculate the 2's complement of -9, and then proceed to take the 2's complement of that first result in order to complete the calculation of the addition of the 'double negative'?

View 2 Replies View Related

C :: Ιnteger Promotions In Bitwise Operators?

Feb 9, 2015

1.The operands from << and >> may be any of integer type (including char) The integer promotions are performed on both operands the result has the type of the left operand after promotion.

It means that if we have z = x >> y then sizeof(z) == sizeof(x) ?

2. The ~ operator is unary the integer promotions are performed on its operand.

So if I have short int y; and int x=1; y = ~x what is the meaning here?

View 8 Replies View Related

C++ :: Reading Bits Using Bitwise Operators From 32 Bit Integer

Jun 1, 2012

I have a 32 bit integer variable with some value (eg: 4545) in it, now I want to read first 8 bits into uint8_t and second 8 bits into another uint8_t and so on till the last 8 bits.

I am thinking of using bitwise operators...

View 6 Replies View Related

C :: How To Show All The Bits Of A Number Using Bitwise Shift Operator

Mar 30, 2013

how to show all the bits of a number using bitwise shift operator....and hence represent the number in 2's complement representation

View 1 Replies View Related

C++ :: Rational Number Class Implementation - Overloaded Operators

Sep 13, 2013

I'm implementing a rational number class:

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

typedef int64_t RAT_INT;
struct RAT{
RAT_INT Num, Den;

RAT(RAT_INT num = 0, RAT_INT den = 1){
Num = num;
Den = den;

[Code].....

Two questions:
1) In the second line in main, how does C++ know to convert 2 to the appropriate RAT?
2) Is it possible to make the third line in main valid without adding global operators for all the member operators to support plain integers?

View 5 Replies View Related

C :: Assigning Array With A Variable Number Of Elements

Jul 18, 2014

I have a simple problem about memory allocation.In the function Nr_elements() i assign a value which represent the elements of array. The pointer p is initialised with the address of variable n, but when i compile i dont know why but dont work. This function return a pointer.

Code:
#include<stdio.h>
#include<stdlib.h>
int *Nr_elements();
int *allocate(int);
void deallocate(int *);
[code]....

View 3 Replies View Related

C++ :: Assigning Prime Number To Element Of Array

Apr 12, 2014

I want to assign prime number to the element of an array.

#include "stdafx.h"
#include <iostream>
#include <conio.h> //For Console Input/Output function like clrscr & getche
using namespace std; //Using the "standard" namespace of iostream class

[Code] ....

View 2 Replies View Related

C++ :: Number Of Characters / Operators / Uppercase Letters And Numerical Digits

May 5, 2014

I have to code a simple program who determining the number of Characters (A character could be any alphabets, digits, punctuation marks, or special , Operators ( Operators are those symbols that are used in mathematica expression, such as,'+', '*', '/', '-', and so on.), Uppercase letters (Uppercase characters are those from A..Z) and Numerical digits ( A digit is any of the Hindu-Arabic numerals from 0..9). Why the output is wrong!

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std ;
int main() {
char text;

[Code] .....

This is my input file This is a possible factorial function in a programming language called LISP

(defun factorial (n)
(if (< n 2)
1
(* n (factorial (1- n)))))

This is my output:

The number of characters = 113
The number of operators = 3
The number of numerical digits = 3
Uppercase letters = 5

I think that "characters" is wrong, but I do not know why !

View 4 Replies View Related

C/C++ :: Open A File And Use Overloaded Operators For Complex Number Class

Apr 10, 2015

This code is meant to open a file and use overloaded operators for a complex number class. I am getting a lot of errors in my class declaration/definition but I am not sure why.

#include <iostream>
#include <cmath>
#include <fstream>
class CN {
public:
double real;
double im;

[Code] ....

View 5 Replies View Related

C :: Significance Of Int Casting In Program With Array Of Structures

Jun 18, 2013

I am doing an exercise which has to do with International country codes.The user must give a code and the programm will display the corresponding country.

Code:
#include <stdio.h>
#define COUNTRY_COUNT
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
[code]....

View 14 Replies View Related

C++ :: Quadratic Formula Loss Of Significance And Simplification

Feb 16, 2015

write codes that could solve a quadratic formula, and my codes are like this:

#include <bjarne/std_lib_facilities.h>
int main()
{
cout << "Enter the coefficients of a quadratic polynomial a*x**2 + b*x +c:
";
cout << " a? ";
double a;
cin >> a;
cout << " b? ";

[code]....

Which runs perfectly, but I have 2 questions:

1. How to simplify these code? On the assignment sheet the professor wrote about using void solve_linear(double b, double c); and void solve_ quadratic (double a, double b, double c);which I currently dont understand how these works. He asked us to write a well-encapsulated (as short as possible) program.

2. These are for extra points: the precision problem of floating numbers: professor asked us to find a way to get the precise answer of it, like this: Enter the coefficients of a quadratic polynomial a*x**2 + b*x +c:
a? 1
b? -20000
c? 1.5e-9
Trying to solve the quadratic equation 1*x*x + -20000*x + 1.5e-09 == 0
Using classical formula: Two roots, x = 20000 and x = 0
Using stable formula: Two roots, x = 20000 and x = 7.5e-14

My guess is that
A. Using code like if(x1*x2=a/c) to check if numbers were approximated.
B. Somehow determine the larger one in x1 and x2.
C. Somehow use that larger one to do something

View 2 Replies View Related

C :: Calculation Of A Bitwise Expression

Mar 6, 2015

I would like to ask about how we calculates the following bitwise expression.

Code:

unsigned char ch[2] = {0x49,0x49};
ch[0] | ch[1] << 8; I'm thinking ch[1] << 8 as 0x00 ...

So, I think that the above expression converts to 0x49 | 0x00 ... and the complete expression should be 0x49 for me.

But, the compiler gives me the result of 0x4949 as two bytes.How does the compiler calculate this expression as two bytes?show me the steps included in the calculation of this expression?

View 2 Replies View Related

C :: Decimal To Binary Using Bitwise

Nov 15, 2013

How to do this program i can easily do it in a simple for loop but i have to do this program with the following directions:

1. Write a function called bitN() that returns the value of bit N in number, where number is the first parameter, and N is the second. Assume N of the least significant bit is zero and that both parameters are unsigned int's. (A simple one-liner will suffice)

2. Write a main() function that uses bitN() to convert a decimal integer into its binary equivalent. Obtain the integer to convert from the first command-line argument.

3. Use the expression
unsigned int numBits = sizeof(unsigned int)*CHAR_BIT;
to get the number of bits in an unsigned int. (Include limits.h to get the definition for CHAR_BIT.)

View 2 Replies View Related

C++ ::  bitwise Operations On Numbers

Dec 8, 2014

Trying to write 4 bytes ints in a binary file and extract them after... I'm using the exclusive or (^) to isolate single bytes to write to and extract from the file since the write() function accepts only chars, only the beginning and end results are not the same...

#include <iostream>
#include <ctime>
#include <fstream>
#include <cstdlib>
using namespace std;

[Code] .....

View 3 Replies View Related

C++ :: Error With Tilde Bitwise Operator

Feb 2, 2015

Let's examine the code.

int x = 100;
unsigned long answer1 = ~x;
unsigned long long answer2 = ~x;
cout << (bitset<32>) x << "

[Code] .....

Shouldn't the decimal of answer 1 and 2 the same thing?

I get 4294967195 for answer1 and 18446744073709551515 for answer 2.

View 1 Replies View Related

C++ :: Incorrect Result In Bitwise Operations?

Oct 30, 2014

I'm doing a bitwise operations on 2 bytes in a buffer, then storing the result in a variable. However, I sometimes get a non-zero value for the variable even though I'm expecting a zero value.

The relevant portion of the code is as follows.

unsigned int result = 0;
long j = 0, length;
unsigned char *data;
data = (unsigned char *)malloc(sizeof(unsigned char)*800000);

[Code] ......

I'm expecting result to be zero when my data[j] and data[j+1] are 0xb6 and 0xab respectively, which is the case for most of the time. However, for certain values of j, my result is strangely not zero.

j = 62910, result = 64
j = 78670, result = 64
j = 100594, result = 64
j = 165658, result = 512
j = 247990, result = 128
j = 268330, result = 512
j = 326754, result = 1
j = 415874, result = 256
j = 456654, result = 1024
j = 477366, result = 512

It appears that these strange result values are all powers of 2, with a 1 bit appearing somewhere in the unsigned int.

I'm not changing the value of result anywhere else in the code, and when I print out (unsigned int)(((data[j]^0xb6)<<8)|(data[j+1]^0xab)), I get 0, but somehow when it gets stored in result, it's no longer zero.

View 3 Replies View Related

C++ :: Bitwise Shift Operator - Size Of Integer

Feb 24, 2012

I have a doubt in Left Shift Operator

int i = 1;
i <<= (sizeof (int) *8);
cout << i;

It prints 1.

Doubt:

i has been initialized to 1.

And while moving the bits till the size of the integer, it fills the LSB with 0's and as 1 crosses the limit of integer, i was expecting the output to be 0.

How and Why it is 1?

View 2 Replies View Related

CLI :: Unable To Read CPP Parameter Definition Using Bitwise Operator

Jul 16, 2014

In a .h file there is a function that takes in this parameter:

void (^callback)(float * arg)=NULL

as in a function definition:

void func(void (^callback)(float * arg)=NULL);

What I am able to read is that it takes a function pointer and if not defined it overrides with NULL. The part I do not get is the ^ in (^callback). I only know ^ as a bitwise XOR operator. It also generates issues in my VS2012 compiler (something with CLR). So I would really like to rewrite this part to something else, without the bitwise operator...

View 2 Replies View Related

C :: Bitwise Operations On 2 Bytes In A Buffer Then Storing Result In Variable

Oct 30, 2014

I'm doing a bitwise operations on 2 bytes in a buffer, then storing the result in a variable. However, I sometimes get a non-zero value for the variable even though I'm expecting a zero value. The relevant portion of the code is as follows.

Code:

unsigned int result = 0;
long j = 0, length;
unsigned char *data;
data = (unsigned char *)malloc(sizeof(unsigned char)*800000);

[Code] ....

I'm expecting result to be zero when my data[j] and data[j+1] are 0xb6 and 0xab respectively, which is the case for most of the time. However, for certain values of j, my result is strangely not zero.

Code:

j = 62910, result = 64
j = 78670, result = 64
j = 100594, result = 64
j = 165658, result = 512
j = 247990, result = 128
j = 268330, result = 512
j = 326754, result = 1
j = 415874, result = 256
j = 456654, result = 1024
j = 477366, result = 512

It appears that these strange result values are all powers of 2, with a 1 bit appearing somewhere in the unsigned int.

I'm not changing the value of result anywhere else in the code, and when I print out

Code: (unsigned int)(((data[j]^0xb6)<<8)|(data[j+1]^0xab))

I get 0, but somehow when it gets stored in result, it's no longer zero. I really don't understand what could be wrong.

View 1 Replies View Related

C :: Associtivity Of Operators

Jun 17, 2014

The + operator has left to right associativity then for the program f1() + f2(), why f1() is not called first compared to f2()?

View 5 Replies View Related

C++ :: Using Logical Operators Like And Or

Dec 20, 2013

I wrote this program using an online compiler i am getting a lot of errors.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string birthmonth;
string birthday;

[Code] ....

View 6 Replies View Related

C++ :: Parallel OMP With Many New Operators?

Jul 14, 2012

I have a code that uses multi threads using OMP. However, Inside the code I allocate too many memory using "new".

I know that the memory allocation using new must be done in serial. Therefore I am not getting a good performance out of the multi-threaded program.

I thought about allocating a big memory only once, and then advance the pointer every time I need to allocate a small memory.

For example, if I need to allocate chunks of 7 doubles:

Code:
main_memory = new double [10000];
double* x1 = main_memory;
main_memory+= 7;
double* x2 = main_memory;
main_memory+= 7

However, I do not know how to handle the memory deallocation. Beside, this is not thread safe method because main_memory is shared by all threads.

May be there is a boost library that does this, which I am not aware of.

View 3 Replies View Related

C++ :: Overloading Subscripting Operators?

Nov 7, 2014

I have two class GameOfLife and Cell and i want to overload square braket for class GameOfLife."if g is a GameOfLife object, g[10][5] will return the Cell at row 10 and column 5. If there is no such Cells, then it will return a new Cell with position (-1000,- 1000)."

but if g[10][1000] and 1000>cols,then it returns different Cell exp (3,2) How i do control the col ( [row][col] )?

Code: vector<Cell> GameOfLife::operator [](const int row){
vector<Cell> rowCell;
for(int i=0; i<cols; ++i)
{
if( isLive(row,i) )
rowCell.push_back( Cell(row,i) );
else
rowCell.push_back( Cell(-1000,-1000) );
}
return rowCell;
}

View 6 Replies View Related

C :: Precedence Of Relational Operators

Apr 3, 2013

Came across the foll code:

#include<stdio.h>
main() {
int i=4,j=7;
j=j||(printf("you can")&&(i=5));
printf("%d %d",i,j);
}

output: 4 1

Although I am specifying the braces for the && operator so that it gets executed first..Then also the value of i remains 4 only..Why does not it gets changed to 5??Also the printf does not execute??

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved