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
ADVERTISEMENT
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
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
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
View Related
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
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
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
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
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
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
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
View Related
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
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
Apr 6, 2014
I have this simple program below:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long long int address;
float current;
unsigned char pressure_units;
} sensor;
[Code]...
As you can see, it has stored the last struct in all three indexes of the array. Why?
View 1 Replies
View Related
May 24, 2013
i having a code to pass in array as argument, but the length returned is 1. This is not match with the array size.
int Getsize(int Array[])
{
int len = sizeof(Array)/sizeof(int);
cout << len << "
";
}
int main()
{
int X[] = {45, 12, 54, 83, 41, 36};
getsize(X);
}
View 19 Replies
View Related
Feb 9, 2014
Currently I am trying to convert RGB to HSL. Everything is working but the saturation value. It is always close to the correct value (usually less than 10 off). For example:
RGB:(196,72,84)
HSL:(354,-46,52)
Correct HSL(354,51,53)
CODE:
double s=0;
double l=0;
chroma=max-min; //works correctly
//LIGHTNESS
l=(max+min)/2;
//SATURATION
if(chroma==0) {s=0;}
else {s=chroma/(1-fabs(2*l-1));}
s=s*100.000000;
l=(l/255.000000)*100.000000;
View 5 Replies
View Related
Apr 25, 2015
I am writing a program where I read in data from a file into an array and a 2D array. However, when I cout that data to insure that it was all read in correctly, I get only the first full line of that input file(where there are actually 25 rows and 12 columns).
What am I doing wrong or should be doing differently?
ifstream fin;
//open the input file
fin.open("store_data.txt");
//If input file was opened, read input file data into array and 2d array
if(fin){
[code]....
View 1 Replies
View Related
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
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
Apr 25, 2013
I'm writing a program to read in a Master.txt file and then update it through a Transaction.txt file that contains various transaction types [Adds (A), Deletes (D), and Edits (E1-E4)]. The records in both files are in ascending order based on Item#. Ultimately, the original Master.txt and updated Master file (Master2.txt) will be merged to reflect all valid transactions, and an errorLog.txt file will be created to indicate all invalid transactions. I feel I have all of the code written correctly, but I am still getting errors on my operands and identifiers.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct invRecord {
string delDate;
[Code] ....
View 7 Replies
View Related
Aug 6, 2014
PROGRAM:-
#include<fstream.h> //for reading and writing files
#include<conio.h> //for clrscr()
#include<string.h> //for string characters
#include<stdio.h> //for gets and puts function
#include<process.h> //for exit function
#include<iomanip.h> //for setw function
#include<dos.h> //for delay and sleep function
void main()
{ char ch,c=0,che=16;
int i=1,j=16;
[Code] .....
View 6 Replies
View Related
Oct 30, 2013
Were are to implement a method countValue() that counts the number of times an item occurs in a linked list. Remember to use the STL <list>
int countValue(list<int> front ,const int item);
Generate 20 random numbers in the range of 0 to 4, and insert each number in the linked list. Output the list by using a method which you would call writeLinkedList which you would add to the ListP.cpp.
In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.
Remember that all the above is to be included in the file ListP.ccp
The output should be:
2 3 4 0 1 0 2 4 2 3 3 4 3 3 3 0 0 2 0 2
0 : 5, 1 : 1, 2 : 5, 3 : 6, 4 : 3
but I am getting:
1 2 4 0 4 4 3 3 2 4 0 0 1 2 1 1 0 2 2 1
0 : 4, 1 : 5, 2 : 5, 3 : 2, 4 : 4,
Here is my code:
#include<iostream>
#include<list>
using namespace std;
[Code].....
View 1 Replies
View Related
Oct 19, 2014
Program: I have 2 arrays: 1 for the correct answers to a quiz, 1 for the user. I then have a vector to hold the incorrect answers. It keeps outputting what looks like alt characters, why.
Here is the code:
#include <iostream>
#include <vector>
using namespace std;
int main()
[Code] .....
View 2 Replies
View Related
Nov 21, 2014
My loop is outputting data incorrectly. I have inbound web data that come in sets of 7. I am trying to in insert the 7 records into a vector and then display the vector content followed by a new line.
Code:
char buff[BUFSIZ];
FILE *fp = stdout;
char * cstr = buff;
vector<std::string> vrecords;
while(std::fgets(buff, sizeof buff, fp) != NULL){
for(int i = 0; i < 6; ++i){
[Code] ....
expected output:
Found buy!
198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
[Code] ....
View 14 Replies
View Related
Mar 10, 2013
We have to make a code to detect the frequency of printable characters. But when I run the code, sometimes it can't detect the Uppercase Letters. But it can sometimes. It's a bit buggy, and it really can't get the frequency of the space character.
Does textcolor affect the outcome?
Code:
#include<stdio.h>#include<conio.h>
int main ()
{
clrscr();
char name[40],sentence[1000],ch;
int c=0,count[95]={0};
textcolor(LIGHTCYAN);
[Code] ....
I also noticed that it stops detecting the frequency when there is a space between character/s.
View 3 Replies
View Related
Feb 9, 2015
I'd wrote a program to encrypt a message within a bmp file using my own structs and all for everything (yes, call me a ........head) The program works but for some weird ........ing reason I was forced to subtract 2 bytes from the header size to get the correct value. I've narrowed down the issue to my BmpFileHeader struct.
Here's a short program that demonstrates the issue:
Code:
#include <stdio.h>
#include <stdlib.h>
#define BYTE unsigned char
#define WORD unsigned short
#define DWORD unsigned long
#define LONG signed int
[Code] .....
Tried with both gcc and TinyCC and got the same result so it doesent seem to be a compiler bug. Microsoft's structures though are giving the correct size, even though they have the exact same definition.
Microsoft's defines:
Code:
// windef.h
typedef unsigned long DWORD;
typedef unsigned char BYTE;
typedef unsigned short WORD;
[Code] .....
View 5 Replies
View Related