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
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.
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
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
Nov 27, 2012
Why this class doesn't work for Subtraction, Division and Square Root. When I try to get results for Subtraction, it output right answer, but with any trash. And When I try to get answer for Division and Square Root, it just brakes and don't output anything. For Addition and Multiplication class work correctly.
Its the link to this class [URL]
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
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
Mar 12, 2014
I'm used to writing enums for enumerated types. However I have this:
enum Colour
{
BLUE = 0x01,
BLACK = 0x02,
WHITE = 0x04,
GREEN = 0x08,
RED = 0x10
};
Now each of the enumerated types are not exclusive, so I'd like to do this:
Colour c = BLUE | BLACK;
Of course that doesn't work. gcc gives "invalid conversion from 'int' to 'Colour' [-fpermissive]".
typedef int Colour;
static const Colour BLUE = 0x01;
static const Colour BLACK = 0x02;
static const Colour WHITE = 0x04;
static const Colour GREEN = 0x08;
static const Colour RED = 0x10;
View 2 Replies
View Related
Feb 4, 2014
So I think I am having syntactical problem with my code.
Code:
int main() {
vector<int> ivec;
int score;
[Code] ....
I get an error from my compiler on the ?10th? line (Nested condition line) that says |19|error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'|
The purpose of the program is to take input and store it in a vector and then change the value to be between 1-6. I made this for the purpose of learning about nested conditional operations.
View 3 Replies
View Related
Mar 16, 2014
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float i;
i=0.7;
[Code] ....
If i do run the above program in turbo C/C++ complier, it outputs "h". But,if i change the code as i=0.6 and if (i<0.6), it outputs "w". Even if i change it to i=0.8 and if(i<0.8), then also it outputs "w".
View 4 Replies
View Related
May 29, 2013
I've been thinking over this for long time... For example, when a button is clicked, print a string "clicked" after 5 seconds. (the button won't be blocked)
Thread should be the only way to do this:
btn.on_click = []() {
thread thrd([]() { sleep_5_seconds(); cout << "clicked" << endl; });
thrd.start();
};
the local variable thrd is destructed right after it starts, which may cause a crash, so use new operator:
btn.on_click = []() {
thread* thrd = new thread([]() { sleep_5_seconds << "clicked" << endl; });
thrd->start();
};
The thrd never get deleted
How would you solve problem like this?
View 2 Replies
View Related
Oct 7, 2014
int main()
{
char rORc, choice;
int sizerc;
[Code]....
when I input the character which is the underscore, and the row # it should display the table and sort that specific row. Why is the if statement skipped? This is not the complete program but has everything needed.
View 2 Replies
View Related
Oct 12, 2012
I've sometimes encountered unexpected runtime issues caused by unsigned values being decremented below zero.
Example 1: "unsigned_value += negative_integer_value;"
Example 2: "for( size_t i = size - 1; i >= 0; --i )"
My compiler doesn't provide any compile-time or run-time warnings.
As far as I know, it's not possible to overload operators of primitive data types to check if the unsigned value is decremented below zero.
Any clever strategy to trace such cases at debug runtime, without having to add asserts all over the code? It's important that it does not affect performance in release mode.
View 6 Replies
View Related
Aug 25, 2013
I intend to reference this thread from another thread so I do not clutter that thread with code
/* This code is relied on by some of the functions I will post */
typedef unsigned long long zxumax;
typedef signed long long zxsmax;
[Code]....
View 13 Replies
View Related
Feb 11, 2015
I'm working on a class project, and I'm having a difficulty. Suppose I have: string a = "21" and string b = "30"; normally, a+b=2130 (i.e concatenation of the characters in the string) but suppose I want a+b=51 (i.e. numerical addition) how do I go about this?
View 1 Replies
View Related
Oct 27, 2012
How we can write coloured text to a text file in c? Can I use cprintf funtion with any kind editing
View 1 Replies
View Related
Oct 24, 2013
Here's the code I'm writing:
Code:
#include <stdio.h>
#include <stdlib.h>
char* getTeamCode(char* team);
[Code]....
Why the placement of the code on line 21 above matters.
It grabs the correct string just fine. If I write a printf just below it, it prints the string it should correctly. However, if I do a printf of the string OSUteamCode below the fopen call on line 23, it prints blank.
So the first thing I did was move it below the fopen line. It worked, finding and outputting the first game in "game.csv" just fine, but not the other 11. Debugging with printf shows that the contents of OSUteamCode again disappear after the fopen call in the addLineCSV function.
I'm not understanding why that happens. The only thing I can figure is there's something going on with the file I/O commands that I just don't understand, but I can't find anything online that explains what that might be.
View 3 Replies
View Related
Jan 9, 2015
I just started learning C++ a week ago and have been stuck on a project for the past 2 days now. I am building a limited purpose calculator which finds the value of one of five operations. Visual studio doesn't underline any errors in my program but every time I try to run it I get an error message. I believe it has something to do with the if/else but Im not sure.
#include <iostream>
using namespace std;
int main(){
int a;
int b;
int sum = a + b;
[Code] ....
View 5 Replies
View Related