# Bit Manipulation Questions

Printable View

• 08-11-2003
CPPNewbie
Bit Manipulation Questions
Hello everyone, i've been reading many tutorials on bit manipulation and advanced bit manipulation. As seeing im new these may seem like funny questions but have in mind I am serious.

When working with the bit wise operators (^, |, &, ~) what are you exactly doing? Comparing the two bytes or individual bits and if so for what purpose. I have seen this done for encryption programs exspecially using the exclusive or because it automatically flips every bit around, but in that case do certain bit patterns represent certain characters or (I do know integers of course, its binary). I have seen that many decryption programs use direct bit manipulation to find encapsulated data and there it is again, are certain characters programmed represented by certain digits? say 0001 = a? Im still pretty much a poor sappy newbie but help is appreciated =D. Also much explanation one what bits are used for today helps and how it relates to data.
• 08-11-2003
7stud
Bits are used to send more than one piece of data in a variable. Say you have the number:

10111

The first bit might signify colored background, the second bit might signify minimized or maximized, the third bit might be scroll bars, etc., with 1 signifying yes, and 0 no. Bitwise manipulators are used to perform operations on the bits: find out what the bits are at certain positions, switch them from yes to no and vice versa, etc.

My advice: new people should skip bitwise manipulaters. Nothing you will study about C++ from here on out will ever rely on knowing bitwise manipulators.
• 08-11-2003
Perspective
>> Nothing you will study about C++ from here on out will ever rely on knowing bitwise manipulators.

i think that statement might be a little extreme. For example, extendable bitwise hashing is a very popular method of hashing. encryption, as mentioned above, also makes good use of bitwise operators. You are right about newbie study though, bitwise operators are more than you need at first.
• 08-11-2003
7stud
Sorry, I should have said in beginning to intermediate C++.
• 08-11-2003
CPPNewbie
Thank you very much =D.
• 08-12-2003
DougDbug
Hardware, Firmware, Embedded Systems.
All data inside the computer is in binary format. Most of the time, the compiler and operating system take care of the conversion, so neither the programmer or user really cares about that. I was amazed when I discovered that there is no binary "built-into" C, or C++.

I mostly work with hardware... I use binary in my test programs for troubleshooting the hardware. A single bit might indicate the state of a switch, or a single bit might turn-on an LED. You can use an oscilloscope to see the high/low (1 or 0) state of a port. (The data on the data bus is always changing, so the 'scope won't tell you anything, unless the signal is stuck high or low.)

Programmers who write "firmware"* for embedded systems, or who write drivers, often deal in binary. Bytes and single-bits in a "register"* often indicate the status of something, or come configuration setting.

All of this bitwise programming is done using hexadecimal, because it is easy (for us humans) to convert between hex and binary.

*FIRMWARE: Software that is semi-permanently stored in an EPROM or flash.

*REGISTER: One or more bytes of fixed-address memory, reserved for a particular use, often located physically on a daughter-card, in a peripheral device, or inside a microprocessor. Not part of the regular RAM... Not part of the heap.
• 08-12-2003
CPPNewbie
Yes I read some articles on Diodes and LED's as well as Boolean Gates. Most of which used bits to represent data or as you said "switch on an LED". So on a physical level what is a bit? An electrical current moved around to a final destination, routed through boolean gates? I really also wanted to know what "memory" is on a physical level. Since computers were built up physically untill we reached the point of virtual memory. Im kind of an amatuer (only 15 -=( ) so my knowledge is a bit limited =D. Thank you all for your help though, and 7Stud sorry for posting on another forum with the same question, no one at CPP-Home.com was responding at the time.

::Edit:: - By the way, Perspective nice avatar. Looks really nice when your overtired and its 3:00 A.M. ~_^
• 08-12-2003
DougDbug
Physical Memory...
Quote:

what "memory" is on a physical level.
Hmmm...
Well, computers still have physical memory. Virtual memory means that if I read the byte or word at... say address 1000, maybe I'm really reading from address 2000, or from the hard disk. If you write to some address via a pointer, the virtual memory might move the data to make room for another program or something... But the operating system can still find it by re-directing the pointer. Confusing Eh?

When you studied gates, you probably studied flip-flops (I hope). A flip-flop can hold it's state, so if you add some more circuitry to test (read) its state, you have a one-bit memory.

A ROM (or EPROM) chip might have 8 address lines (pins) and 8 data lines. Inside it has a bunch of "latches" (like flip-flops). When you put some bit-pattern on the address lines, (and turn-on the enable line)... the data stored at that address will appear on the data lines. If you hold the address/enable lines instead of changing them at MHz and GHz rates, you can read the data with a 'scope or meter. If address zero contains 55 Hex, and you put 0000 0000 on the address lines, you will find 0101 0101 (=55 hex) on the address lines. Put 0000 0001 on the address lines, and whatever data is stored at address 1 will appear on the data lines.

You can't check a bit inside memory with a 'scope or meter, you have to get it on the bus. And, you can't statically read/write RAM or ROM like I described while the processor is running.... But you can put it into a loop and read/write to the same address over and over. (That get's tricky too 'cause it also has to read the instructions telling it to loop.)

You can find more info about memory (and lots of other technical stuff) at ePanarama.net

If you need clarification, or have any more questions for me, send me a PM (Private Message) 'cause we're getting off the C++ topic for this board. :)