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


ADVERTISEMENT

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++ :: 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++ :: 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 :: 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 :: 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 :: 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 View Related

C/C++ :: Reverse Of A Number?

Mar 28, 2014

I have written a code to find reverse of a number and print if if the original and reversed numbers are equal or not .the problem is in the if condition where the first printf is not working at all. the code is

#include <stdio.h>
#include <conio.h>
int main (void)
{

[Code]....

as if i enter 121 it still shows the 2nd printf instead of 1st one. sort out the bug;;!

View 4 Replies View Related

C/C++ :: Reverse Fibonacci Sequence - N Number In Series

Sep 3, 2014

When ever I enter a value higher than 10, my out put looks like this

How many numbers are in the Fibonacci Series? 20

0 0 0 0 0 0
0 0 0 0 34 21
13 8 5 3 2 1
1 0
Press any key to continue . . .

This is what I got when I entered the value 20.

As you can see, it will only display up to the 10 term of the Fibonacci sequence

I'm trying to get the output to look like this:

4181 2584 1597 987 610 377
233 144 89 55 34 21
13 8 5 3 2 1
1 0

Here is my code:

#include <iostream>
#include <iomanip>
using namespace std;
#define SIZE 20
void Fibonacci( int );
int main( ) {
cout << "How many numbers are in the Fibonacci Series? ";

[code].....

View 2 Replies View Related

C :: Program To Count Number Of Lines In Text File And Reverse Contents

Jan 25, 2015

C program to count the number of lines in a text file and reverse the contents in the file to write in an output file.

ex :
input
one
two
three

output:
three
two
one

View 6 Replies View Related

C++ :: Assigning Bits A Value Of 0

Feb 12, 2014

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.

View 5 Replies View Related

C++ :: Getting Bits In RDX After Multiply (x64)

Aug 13, 2013

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?

View 4 Replies View Related

C++ :: Shifting Bits Away And Back

Mar 25, 2014

Value x is a 32-bit unsigned integer 3:

00000000000000000000000000000011

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?

View 2 Replies View Related

C++ :: How To Set Certain Bits Of Unsigned Short

Aug 8, 2013

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.

View 4 Replies View Related

C++ :: How To Write Bits To A Disk

Mar 30, 2014

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?

View 3 Replies View Related

C/C++ :: How To Locate The Lowest 8 Bits In A 64 Bit Key

Jul 15, 2014

I am trying to encrypt a plaintext using DES in C. I read about the algorithm and how it works, but when i came to write the code i struggled. :

How to locate the lowest 8-bits in a 64-bit key ?

How to shuffle the plaintext according to the algorithm description ? (I read about bitwise operations, but i still cannot understand how i can use them to transfer for example the 5th bit to the location of the 30th bit)

Left shifting the key would not wrap the bits, so i just bitwise or with a mask that will add the bits that did not wrap around?

View 1 Replies View Related

C/C++ :: Add 8 Bits - Return The Character

Oct 10, 2014

I'm working on an assignment where I have to read an image in the PPM format. This format consists of a header that contains the parameters and the rest is raw bits.

I have to work with a modified PPM image that contains a secret message. This message is stored in the first X number of bytes. To decode a single character I would have to look at the lowest level bit of 8 bytes and return that as a character, then repeat this for the length of the message.

The code below is what I have so far, but I do not get the expected output but instead I get smiley-faces.... or other ASCII characters depending on the shift.

char buffer = 0;
int MsgSize =(size*8);
int nRead = 0;
printf("The secret message is displayed below: ");
for(i; i<MsgSize; i++) {

[Code] ....

View 5 Replies View Related

C# :: Sbyte To Int - Copy Only Bits

Jan 15, 2014

I just have a short question! I have an sbyte and I want to convert it to an int, but I do not want a value conversion, just to copy the bits, such that the negative numbers in the sbyte will be their complement in the int (-12 in sbyte -> 244 in int)... How can I do that?las

View 1 Replies View Related

C :: Decimal To Binary Converter (8-bits)

Sep 9, 2014

I was trying to program an decimal to binary converter (8-bits) in C. I am a complete beginner so I tried to put the 1's and 0's of the binary number as they come without reversing the order for beginning. I have seen example on the internet but didn't understand them so I decided to write it as I understood it. So, I typed the code as shown below:

Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int number;
int BitNum[8], x;

[Code] ....

The problem with the code is that if binary form has 0s in it then program displays a random number instead of a 0. For example if decimal is 7, it should print out 11100000 but it displays only 111(and some stupid numbers instead of 0). I have tried to solve it but failed.

View 7 Replies View Related

C :: Storing Multiple Bits As One Integer

Oct 3, 2013

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++)

[Code]...

View 9 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++ :: How To Manipulate Bits Of All Types For Storing

Aug 10, 2013

I need a translate (in both directions) all primitive types, into char[] (will be stored in string)

I understand how to manipulate integral types with bits and I cant just cut them down and shift them, but float and double don't work with this manipulation. So, how I can create a perfect bit copy of float and double?

int i = 0xFCED03A4; //Random number
char c[4];
c[0] = ((i >> 3) & 0xFF);
c[1] = ((i >> 2) & 0xFF);
c[2] = ((i >> 1) & 0xFF);
c[1] = (i & 0xFF);

[Code]...

This is basic stuff but I need an equivalent for float and double types, and it needs to be a perfect BIT copy, not value copy.

View 7 Replies View Related

C++ :: How Many Bits Are Created By Unsigned Exponent

Nov 21, 2013

i want to ask how many bits are created when write this line

unsigned exponent:10;

is exponent locate a 10 byte from memory or 10 bits ?

View 1 Replies View Related

C++ :: Setting All Bits Within Signed Int With Exception To MSB

Dec 26, 2013

I tried to write a little bit of code to set all bits within a signed int with exception to the MSB, yielding the greatest max positive value. The odd part is that it works for shorts ints, and longs, which are 2, 4 and 4 bytes respectively, however long longs, with a size of 8 bytes, simply yields -1, which would indicate that it failed to clear the MSB. Heres the little segment in question:

template<typename T>
T getMax() {
return my::numerics<T>::is_signed ?
~0 ^ (1 << ((sizeof(T)*8)-1) ):
~0;
}

my::numerics is just an exercise- its thoroughly tested and I'm certain thats not the issue.

For shorts, ints, longs, this yields the maximum value. However, when I use it on long longs, the output is 0xFFFFFFFFFFFFFFFF, i.e. ~0. Obviously this means the maximum value for unsigned long longs, but -1 for signed long longs.

View 2 Replies View Related







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