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.
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'?
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?
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.
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.
So I'm supposed to write a code that asks a user for a string and then displays the hex, decimal, and binary code for each individual letter and then tells the user how many bits in binary were 1. For example: Enter a line of text: Hello
The ASCII code for 'H' is 0x48 in hex, 72 in decimal, or 01001000 in binary, 2 bits were set. The ASCII code for 'e' is 0x65 in hex, 101 in decimal, or 01100101 in binary, 4 bits were set. The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set. The ASCII code for 'l' is 0x6c in hex, 108 in decimal, or 01101100 in binary, 4 bits were set. The ASCII code for 'o' is 0x6f in hex, 111 in decimal, or 01101111 in binary, 6 bits were set.
So far I've got a code that will display the binary bit pattern by shifting a mask and testing for a 1 or 0. The problem is I can't figure out how to make it so the 1's and 0's get put into a single integer rather than just printing out. I hope that makes sense. Here's my whole code.
Code:
#include<stdio.h> main () { int i; char input; printf ("Enter ........: "); scanf ("%c", &input); for (i = 1; i <= 8; i++)
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?
I am aiming to read an integer from stdin(pointed to the uart) and echo it with the uart interrupt service routine. there is a simple retarget file along with the main code shown below. So far i can read chars (char x[32] but i am struggling with int.
I have gathered that i need to use the scanf function to read an int from the pointer defined in fgets.
My output is giving me weird values, i enter 8 and ill get a random 3 digits back. I have a feeling its a problem with the input buffer.
wrote this program to check if a string is an integer. It checks for + or - sign at the front of it, but it spat out some errors.I think I broke it.Here is the code:
Code:
#include<stdio.h> #include<ctype.h> #include<stdlib.h> int getInteger(char*); int main(void) { char str[99]; int x; }
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?
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.)
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...
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.
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...
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
I am having a problem assigning bits a value of 0. The data is a 16 bit integer the bits greater than the 12th bit have garbage either a 0 or a 1. I would like to assign all bits greater than 12th bit the value 0 no matter what their values are. Whats the best approach.
Using the old fashioned (unsigned) multiplication instruction in x64 assembly multiplies RAX (64 bit register) by a 64 bit register. The answer is stored in RDX:RAX (i.e. the answer is 128 bits). Is there any way, using native c++ to get the value in RDX (higher 64 bits)? One I can think of is: right/(limit/left) e.g. if we are limited to a byte then 97*123 would overflow:
97/(255/123) = 46 times, which is RDX's (if it was one byte) value. But this is too inefficient. Is there a fast way?
If we use bitwise-shift to shift all bits to the right by 2, x is 0:
00000000000000000000000000000000
If we then do a bitwise leftshift on x by 30, do we end up with:
11000000000000000000000000000000 or 00000000000000000000000000000000
In other words, when we perform right shift which clips away the least most significant bits, and then do a left shift, is it possible for those bits to reappear?
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)?
I have a double variable and depending on certain conditions I need to set certain bits of an unsigned short Variable. For example, if double var is odd I need to set the 15th bit of the unsigned short variable.
I'm trying to write a program that writes data to a disk in C++ without caring about it's file system. Here is what I can do so far:
#include <iostream> #include <unistd.h> #include <fcntl.h> using namespace std; char buffer[] = "Wow! I'm writing this data to a disk without puttting it into a file!"; int main(){ int Disk=open("/dev/sdb",O_RDWR); write(Disk,buffer,sizeof(buffer)); close(Disk); return 0;}
But this program can only write ASCII characters to the disk. But what if I want to mainipulate bits on the disk, how would I do that?