C :: Calculate Number Of Bits In Type - Incorrect Result In Program

Feb 15, 2014

I have this simple program below:

Code:
#include <stdio.h>
#include <limits.h>
unsigned int rightrot(unsigned int x, unsigned int n) {

/* calculate number of bits in type */

size_t s = sizeof(x) * CHAR_BIT;

[Code] ....

The result it prints is 2684356604 on my 32-bit computer. The result I expect is as follows:
0xFF94 is 0000000000000000 1111111110010100 in binary.

Shift it right by 5:
0000000000000000 0000011111111100

Then take that result in shift it right by 27 (s is 32 and p is 5, so the difference is 27):
1111111110000000 0000000000000000

Now we use bitwise or:
0000000000000000 0000011111111100 | 1111111110000000 0000000000000000 = 1111111110000000 0000011111111100

That in decimal is 4286580732. So how does it come up with 2684356604?

View 2 Replies


ADVERTISEMENT

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++ :: Retrieve Double Number - Store 8 Bits Of Number In One Field And 16 Bits In Another

May 14, 2013

I am working on a project where I need to retrive a double number and store 8 bits of the number in one field and the other 16 bits in another field. the code below gives me an error.

lata= lat>>8;
latb = (lat & 0xff);

The error states that & and >> are illegal for double. With this in mind, can I use these on a double. If not what can I do to achieve what I am trying to do?

View 1 Replies View Related

C/C++ :: Calculate Result Of 2 Numbers That User Entered

Oct 29, 2012

In this exercise, write a rational number calculator that ask the user to enter two rational number:

a/b + c/d

and produce the result:

(ad+bc)/bd

View 10 Replies View Related

C++ :: How To Write A Program To Calculate Pi To N Number Of Places

Aug 13, 2014

I assume floating point numbers in C++ have a default maximum of 5 decimal places. You can use setprecision() but is this limitless?

So how would find say write a program to calculate Pi to N number of places?

View 3 Replies View Related

C++ :: Program To Calculate A Grade Letter Into Number?

Feb 7, 2013

So I have to write a program to calculate a grade letter into a number.

Letter grades are A, B, C, D, and F, possibly followed by + or –. Their numeric values are 4, 3, 2, 1, and 0. There is no F+ or F–. A + increases the numeric value by 0.3, a – decreases it by 0.3. If the letter grade is illegal (such as "Z"), then your output should be "INVALID LETTER GRADE"; If the combination is illegal (such as "A+" or "F-") then your output should be "INVALID GRADE COMBINATION"

Also the code should look like this
Enter your letter grade: C+
Grade value is [2.3]

// Input
cout << "Enter your letter grade: ";
string s;
cin >> s;

[Code].....

View 2 Replies View Related

C++ :: Calculate Number Of Days Between Dates - Program Continuously Runs

Mar 24, 2014

I have an assignment to write a program that will calculate the number of days between two dates (including leap years). We have to read the data from a file and in my current input file I have:

1 1 2000 1 5 2004
5 2 2005 2 5 2009
1 15 1998 1 15 2001

for testing purposes. When I run the program the console runs continuously spouting out "The days between your two dates are 1097." Which would be the correct output for the last set of dates entered, but I need to find out where I've messed up in my main.

#include <iostream>
#include <fstream>
using namespace std;
bool isLeap(int x) {
if (x % 400 == 0) {

[Code] .....

View 7 Replies View Related

C++ :: In What Type Intermediate Result Of Expression Is Stored

Jan 8, 2013

my doubt is :- in what data type the intermediate result of an expression is stored? like for expression 2+3*1/2 , i think the intermediate result for 1/2 is stored in the form 0.5 but for expression 2+3*1/100 , i think the intermediate result for 1/100 is stored in the form 0.01 so i am not sure if the compiler use dynamic type ie, changes with need. or it always stores in high precision like:- for 1/2 its 0.5000 and for 1/100 also 0.0100 or something like that.

View 4 Replies View Related

C/C++ :: What Data Type Is The Result Of Calculation On Int And Double

Jun 24, 2013

okay for instance
int x = 4;
double y = 2;  
printf("%d", (x / y + 5));

what data type is
(x / y + 5)
??

View 3 Replies View Related

C++ :: Dice (with Varying Number Of Sides) Program To Calculate Percentage Of Results

Oct 28, 2013

I am trying to code and compile a program that requests a uses to first inpu the number of sides they wish for a dice to have and secondly input the number of dice throws they would like to calculate the value for. The program will print an error message and default the number of sides the dice has to 5 if the number entered is below 4. Finally it will print out a percentage figure for each value that results from all the the dice rolls.

I have attached my header, driver and class files below along with accompanying error messages below them

// file name Exercise.cpp
#include <iostream>
#include "Die.h"
using namespace std;
int main() // the main program begins here
{
char restart = 'y'; // declare variables
double numRolls = 0.0;
int diceTot = 0;

[Code] .....

View 5 Replies View Related

C++ :: Retrieving First Three Bits Of A Number

Apr 25, 2013

I am trying to retrieve the first three bits of a number. The code that I am using should work but it isn't giving me the correct result when trying certain numbers. Below is the code I am using:

unsigned short num1, num2 = 0;
unsigned short num = 65535// binary 111111111111111
num1 = num && 0x07;// gives me 1 but should give 7(111)
num2 = num >>3;//gives me 8191, which is correct

Why I am not getting the first three correct bits(111)?

View 2 Replies View Related

C++ :: How To Reverse Bits For A Number

Sep 3, 2013

I am working on a project where I need to reverse bits for a number.

For example, if I have 110111100000000 I need to reverse it to 0000000001111011.

View 19 Replies View Related

C++ :: Saving Specific Bits From 64-bit Number

Jun 24, 2013

I have a 64-bit uint64_t number:

Code:
Primitive<uint64_t> b = 0xCCCCCCCC00000000; I need to save the first 31 (most important) bits - 7FFFFFFE.

I found this solution in the Internet:

Code:
start = (((b)>>(first)) & ((1<<(((last+1)-(first))))-1)); but in my case for this code:
Code: Primitive<uint64_t> start = (((b)>>(32)) & ((1<<(((63+1)-(32))))-1));

I get an error: left shift count >= width of type

And even if I change 63 to 62:

Code:
Primitive<uint64_t> start = (((b)>>(32)) & ((1<<(((62+1)-(32))))-1));

I get: error: integer overflow in expression

View 5 Replies View Related

C :: Return Number Of Bits Set To True

Jan 3, 2015

The task is to return the number of bits set to true.Here is my code:

Code:

int cardinalityBS(PBitSet _this) {
int s, counter = 0;
for(s = 0; s < 31; s++) {
counter += _this->bits & 1;
_this->bits <<= 1;
}
return counter;
}

[code]....

The code is not working, since whenever I set _this->bits to a number, it returns me the wrong result.

View 7 Replies View Related

C++ :: Assign Number Of Bits By Variable In Bitset?

Nov 29, 2013

i want to assign number of bits by a variable in bitset? how to do that? like bitset<4> foo; instead of 4 i want to use some variable and later on by user i want to assign it! boost library or any other library!

View 1 Replies View Related

C/C++ :: Incorrect Output From Unsigned Binary To Decimal Program

Feb 2, 2015

This program has to convert an unsigned binary number into a decimal number. No matter what binary number I enter, however, it always outputs that the decimal number is 0.

My code is as follows:

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
string binarynumber;
cout << "Enter an unsigned binary number up to 32 bits." << endl;

[Code] ....

And my output:

Enter an unsigned binary number up to 32 bits.
00001111
That number in decimal is 0

The output should have shown the binary number in decimal to be 15, and I cannot find my error.

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/C++ :: Program To Find Index Of Character In A String Gives Incorrect Output

Oct 28, 2014

I've been typed out a C program to let the user define the size of their string , and key in characters for this string , the program would then prompt the user for a character to search for in the string and return it's index value. Eg. Index of c in abc is 2. My code is as shown:

#include<stdio.h>
#define SIZE 20
int search(char x[SIZE+1] , int n , char s);
int main(void){
char x[SIZE+1] , s;
int n , index;

[Code] ....

However , after I key in my characters for the string , the program does not prompt me to input a character to look for, it just prints it out and returns some funny number. But the program works just fine is I move this portion to the top :

printf("Enter alphabet to find: ");
scanf("%c",&s);

View 1 Replies View Related

C++ :: Program That Add / Subtract / Multiply Or Divide Two Integers - Incorrect Answers

Oct 7, 2012

Create a program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtractions, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one."

And here is the test data. I am posting the results from my desk-check table.

operation first integer second integer answer
A 10 20 30
a 45 15 60
S 65 50 15
s 7 13 6
G -1
M 10 20 200
d 45 15 3
d 50 100 2

Then, I transferred my program into a source file. Here it is:

//Exercise16.cpp - display answer of two integers

#include <iostream>
using namespace std;
int main() {
//declare variables
int firstInteger = 0;

[Code] ....

After putting in the data, everything worked fine, except the last two operations, which are M (multiplication) and D (division). All the answers for the last two operations essentially give me a 0.

View 4 Replies View Related

C :: Get Binary Number And Change Its Bits - Calling A Function Not Working

Apr 8, 2013

This code gets a binary number and change its bits. I have a problem with the "bits_up" function . Why this function not working?

Code:
#include <stdio.h>
#include <stdlib.h>
int bits_up(uint first,uint last,int *ptr);
int main(void)
{
uint first,last,bitUD;
int InputBinNumber[4],updatedNum[4];

[Code] ....

View 5 Replies View Related

C++ :: Can't Output Decimal Number From A Function Result

Mar 28, 2013

I have this simple code here:

Code:
#include <iostream>
int multiply (double x, double y) {
double result = x*y;
return (result);

[Code] ....

I get the answer 5.94 (which is what I'm looking for). I can't work out why the first example is not outputting a decimal number. I have set the variables as a double so I just can't see why this is not working for me.

View 1 Replies View Related

C :: Function That Computes Factorial Result Of Certain Number

Aug 28, 2013

I just want to practice in the language so i wrote this simple function that computes the factorial result of a certain number. The problem is that for all numbers > 20, the results are wrong ( < 20 all good).

I already learned that normal "long" type in c is more like 32 bit int and not 64 bit like a long type in java. so I used here a "long long" type.

why am I getting strange results above the number 20? isn't 64 bit enough to hold those numbers?

Code:
long long factorial(int n);
int main() {
long long result = factorial(20);
printf("%lld",result);

[code] ...

for 21 i get: -4249290049419214848

where the right result shoud be: 51,090,942,171,709,440,000

View 8 Replies View Related

C :: Program To Convert Bits To Bytes

May 13, 2013

I made this program to convert bits to bytes, because I'm so sick of seeing ISP's advertise speeds in megabits, which I consider an intentional attempt to decieve :P And I think I've finally understood how the return value of scanf works since the last time I posted here, so my program can check to see if an integer was entered before processing the input, but I'm stuck on how to make the whole program start over if an integer is not entered. I have a hunch it would involve a loop, but I can't figure out how to make the program start over at "How many mb do you need converted?" if an integer is not entered into scanf..Here is the code I have so far:

Code:

#include <stdio.h>
int main () {
int b, mb, kb, Byte, kB, mB, gB;
char term;
}

[code]....

and my program makes the assumption for now at least, that mb will be inputted because that's the unit of measurement that i usually see advertised, and i didn't bother making an if statement to print a conversion in terms of gigabytes because i've never heard of a connection that fast :P

View 5 Replies View Related

C++ :: Print Out Result Of One Number Being Added By Two Numbers 999999 Times

Feb 17, 2013

How would I by any means print out the result of one number being added by two numbers 999999 times, the numbers both happen to be about 16500 digits long. It must display ALL digits in the number (all the digits the result ends up being). I know I can't use normal data-types, so what do I do?

Looks kinda like this:

for (int i = 0; i < 999999; ++i)
{
total += a;
total += b;
}

a and b being 2 different numbers with about 16500 digits.

View 2 Replies View Related

C++ :: Variable Initiation - Calculate Number Of Multiplications It Took To Reach A Certain Number

Feb 19, 2014

So I have a template, part of a larger code, that is designed to calculate the number of multiplications it took to reach a certain number. The problem is, whenever I execute the program, mults is always printing out a strange number, perhaps its actual address.

template <class T>
T power3(T x, unsigned int n, unsigned int& mults) {
if (n == 0) return 1;
if (n == 1) return x;
if (n == 2){

[Code] ....

View 10 Replies View Related

C# :: Program Keeps Adding 1 To Result?

Oct 21, 2014

I am creating a program that allows the user to enter the number of days worked and calculates the amount of money gained by doubling the amount from the previous day, starting with .01 cents. The program works fine except for in day 3, the program adds .01 along with doubling the amount from day 2. Also I must use a List Box.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

[code]....

View 3 Replies View Related







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