bitwise mov

in visual C++ is there a way to move along a byte one bit at a time and change/display it?
Here's a function from the website http://www.java2s.com/Tutorial/Cpp/0040__Data-Types/Displaythebitswithinabyte.htm. It uses a clever bitwise manipulation :)
1
2
3
4
5
6
7
8
9
10
void show_binary(unsigned int u) 
{ 
  int t; 
 
  for(t=128; t>0; t = t/2) 
    if(u & t) cout << "1 "; 
    else cout << "0 "; 
 
  cout << "\n"; 
}
The << and >> operators will shift a number bitwise to the left or right. If you want to display, for example, the rightmost bit, you could bitwise and (&) the number with a bitmask (like 0x0001).

so you would have:

1
2
int x = 0x1;
x << 1;

will result in x = 0x2.

To flip a bit, simply use bitwise not (~) or xor the the byte with a mask (^).

What exactly are you trying to do?

Last edited on
im trying to manipulate bits of bytes and display them thats all, kinda like a home made hex editor
@ fafner Thanks for the code, and the site! :)
Last edited on
Ok, so say you have:

 
int byte = 0xABCD;


Tell me what you want to do with it, and I'll show you. If you want to display the nth bit, you could do this:


1
2
3
4
5
//displays nth bit of byte
int byte = 0xABCD;
int n = 9; //change this to whichever bit you want to view

cout << ((byte >> (n-1)) & 0x1);


If you want to flip the nth bit, you could do this:

1
2
3
4
5
//flips nth bit of byte
int byte = 0xABCD;
int n = 9; //change this to whichever bit you want to flip

byte = (byte ^ (0x1 << (n-1));


Basically you can use the bitwise operators:
<< >> & | ~ ^
and hex masks:
0xFFFF
0x0
0x1 << n
and ~(0x1 << n)
to do anything you would want with your byte.
Last edited on
write 0 to nth bit:
byte &= ~(1<<n);
write 1 to nth bit
byte |= (1<<n);
flip nth bit:
byte ^= (1<<n);
Last edited on
@RPG....
how about like a vector its dynamic, reads all the bits (grows/shrinks according) displays all bits; side topic, i need something, literature to help me plan how to solve a problem (break it down). then i can code it from there
Last edited on
1
2
3
4
5
6
7
8
9
10
int byte = 0xAB;
vector <bool> bits;

//reads all bits
for (int i=0; i<8; i++)
bits.push_back((byte >> i) & 0x1);

//displays bits from left to right
for (int i=7; i>=0; i--)
cout << bits[i];


Read this to learn more about bitwise operations:
http://www.cprogramming.com/tutorial/bitwise_operators.html

Last edited on
^ Putting the bits into a vector in order to print them? Srsly?

bitset<8> makes everything easier:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bitset>
#include <iostream>
int main()
{
    unsigned char val = '\x1a';
    std::bitset<8> b(val);
    // iterate and change
    for(size_t n = 0; n < b.size(); ++n)
        b[n] = ~b[n];
    // could also work with the whole thing
    b = ~b;
    // print
    std::cout << b << '\n';
}
Last edited on
One advantage with vector<bool> compared to bitset is that with vector<bool> you don't have a fixed limit. You can store 100000 bits if you like :D
He's editing a binary file, he'll probably be using mmap or whatever anyway.
Last edited on
no... i will be including this in my program as a Class or/ function, to program microprocessor, im making a remote controlled app for a toy car
Also remember there's a special template specialization for vector<bool> where they are stored as bits, not bools. So the solution is (space) efficient.
Topic archived. No new replies allowed.