C :: How To Show All The Bits Of A Number Using Bitwise Shift Operator
Mar 30, 2013how to show all the bits of a number using bitwise shift operator....and hence represent the number in 2's complement representation
View 1 Replieshow to show all the bits of a number using bitwise shift operator....and hence represent the number in 2's complement representation
View 1 RepliesI 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?
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...
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?
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.
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 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.
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.
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 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.
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
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.
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 RelatedThis 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] ....
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?
This shows any prime number between a and b and also counts them*/
#include <iostream>
using namespace std;
int main(){
int a;
int b;
[Code] .....
I have been given to do a simple ATM project which is containing of getting a number of notes in the ATM (100, 500, 1000). Get the withdrawal of money and show the number of note. Here what i got so far. I may need to use a loop but I am not sure how to put it into this.
#include <stdio.h>
#include <conio.h>
int main() {
int a=10, b=10, c=10;
int q;
[Code] ....
But the problem is i don't know how to make the code continue with the process with the amount of notes that is left from the previous input. The program will just stop but i really want it to be able to input new withdrawal amount so that it can decrease the note each time i withdrawal the money.
I'm working in a web aplication in c#, vs 2008, framework 3.5, I need to assign a value to a var, in this case "problemas", when I tried to save the value that I have in the checkboxlist, it does, but if don't selected, can't show 0, besides if I change the value "problemas = 9" to problemas = 111111" doesn't works.
protected void ckblProblemas_SelectedIndexChanged(object sender, EventArgs e){
int problemas = 0; {
if (ckblProblemas.SelectedValue.Equals("9")) {
problemas = 9;
} else {
problemas.Equals("0");
}
Question about instead of using the division operator to display the output of user"s input....
View 4 Replies View RelatedIt is possible to change the number of operands an operator takes?
I think it is false. Am I right?
Write c/c++ code of overloading ^operator in complex number class.
If we have two objects of complex number class as fellows,complex obj3 =obj1 ^obj2 ;
Obj3 reak and imaginary pars will be ,
Obj3.real = (obj1.real)obj2.real
And
obj3.img = (obj1.img)obj2.img
I am having this problem were the user types in a substring to search in the main string and replace it with another string
for example:
string: hello
sub string: el
replace: i
should print this: hillo
I tried using some string functions but I am having problems with that.
I need to circular right shift a cstring in C++
Let's say I have unsigned char test[10] = "HELLO!!!"; How would I go about circularly shifting this to the right? Inline assembly instructions would be ok too
I have this BCD string: 890123...78FFF and need to right shift by say 3 nibbles, resulting in: FFF890123...78
length is fixed, number of 'f' nibbles are varying.
when we are trying to shift one tab to other one event should occur, can any one tell what is the event,how and where we have to write the event? give example. if he successfully login in first tab only then second tab will open,if he doesnt login in first and tries to open the second tab message should appear...
View 1 Replies View RelatedIf input value was shifted to the right on bit level. How can I determine whether a logical or arithmetic shift is performed.
Code:
#include <stdio.h>
#include <stdlib.h>
void main ()
[Code]......