Bitwise operators

Questions about programming languages and debugging
Post Reply
scatter
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 366
Joined: 01 Jan 2014, 05:22
10

Bitwise operators

Post by scatter »

Okay I really had a headache trying to understand Bitwise operators of C , I finished learning C and already doing C networking with sockets etc but I ignored the bitwise operator section because maybe because of my english I didn t understand it well so anyone can help with that ? :)

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Bitwise operators

Post by ayu »

Bitwise operations are performed on individual bits and have a number of usages.

Let's take OR as an example.
In the world of electricity it would mean "either one can be turned on" and could be visualized with a room with one lamp in the ceiling, and two buttons and both sides of the room.
You can switch on either of the buttons to turn of the light in the room. This is an or in it's most common and basic form as far as I know.

As you can see in the picture below, either one of the switches can be turned on the achieve the output "1".

Image

So let's leave the world of electricity and circuits and go back to computers :P.
The principle is the same here, just that we operate on bits in a computer instead.
A byte consists of 8 bits, and you can think of each bit as the "output" of the electronics example, or simply the lamp in an on or off state (The state is binary and can be only 1 or 0).

If we take the bits 1 and 0 and "OR" them, then the result would be 1 since "either one can be turned on" to achieve the "on" result.
If we put this in a so called "truth table", it would look like this:

Image
The truth table is just a way to represent what we are doing visually so we can get a better overview and understanding of what's going on.

If we now take a whole byte and OR it with another byte, the same thing would happen, except that when we OR two bytes we do the same operation on all of the bits instead of just two of them like we did before.

We'll take the bytes 10000000 and 00000001.
Oring them would give the result 10000001.

10000000
OR
00000001
=
10000001

Another way to visualize an OR
Image

A common way to apply this in programming, is if you have a function that takes a set of options in the form of a bit "mask" (forget the mask part for now, it's still just a byte we are sending in).
Let's say that every bit in a byte is a switch for a different option for a function. To make it simple we'll only use the three first least significat bits in a byte.
The first bit means "write", second means "read" and third means "execute" (Let's say it's a special function for setting permissions on the file system).

We'll define three constants as "WRITE = 00000001, READ = 00000010, EXECUTE = 00000100".
If we then wanted to have both read and execute permissions set on a file through this function, we would call it like this:

Code: Select all

set_file_permission("somefile.bin", READ | EXECUTE);
Like this we would set the in data to become 00000110.

You can read about the other bitwise operations and apply them similarly.
For example the AND means "both needs to be turned on", which in the case of our circuit from the electrical example, would mean that both of the switches in the room has to be turned on to turn on the light.

If you still don't really understand where you could find any use for this, then don't worry about it :)
There will come a time where you will think "hey, this would be really efficient with bitwise operators" ^^

I hope I didn't make it even more confusing for you :F
"The best place to hide a tree, is in a forest"

scatter
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 366
Joined: 01 Jan 2014, 05:22
10

Re: Bitwise operators

Post by scatter »

wohou thx alot cats that was fucking amazing now I see it clearly ^ ^ much appreciate it man :) btw which books u recommend for C and C networking ? I used K&R for C and practical C programming by O Reilly but still feel there r some probs in some places (maybe coz I didn t practice it yet and still in theory phase ? )

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Bitwise operators

Post by ayu »

scatter wrote:wohou thx alot cats that was fucking amazing now I see it clearly ^ ^ much appreciate it man :) btw which books u recommend for C and C networking ? I used K&R for C and practical C programming by O Reilly but still feel there r some probs in some places (maybe coz I didn t practice it yet and still in theory phase ? )
You are welcome! :D

I use K&R for the basic theory and online tutorials for anything else.
Personally I feel that when you get the basics down you can start playing around with different libraries and frameworks to build something cool.
It also depends on what OS you want to write it for if you are not using some framework to abstract that part away (like boost or qt, although qt is written in C++).

I would suggest getting down to business and do some practical project if you feel that you have the basics settled in.
Depending on what you are interested in, there are a number of cool things you can do :)

You could write a chat client, or maybe a little virus. Use your imagination ;)
"The best place to hide a tree, is in a forest"

scatter
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 366
Joined: 01 Jan 2014, 05:22
10

Re: Bitwise operators

Post by scatter »

am interested in malwares and exploits dev , exploits dev ofc for opensource software because am not done yet with asm ? so what do you suggest for that? I mean any guidance or places to start from? :)

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Bitwise operators

Post by ayu »

scatter wrote:am interested in malwares and exploits dev , exploits dev ofc for opensource software because am not done yet with asm ? so what do you suggest for that? I mean any guidance or places to start from? :)
Well you can get pretty far with exploits dev without knowing all that much assembly, depending of course what kind of exploits you are writing.
Understanding some basic asm will get you a long way without needing the knowledge to actually write any asm (you don't write exploits in asm typically so).

Writing a little backdoor or bot for a botnet and releasing it in a virtual environment is a fun exercise.
You could write a classical trojan that calls home via an IRC server or such.

A good starter is to write a simple program that takes a password.
The password buffer is then handled in an unsafe manner, which opens up for a buffer overflow.
Then write a little simple exploit to bypass the password window or such :)

Another thing that is really good to know, is IDA Pro which is used for disassembling and debugging.
It might be a lot to take in, but just doing a short beginner tutorial about IDA will teach you a lot that you will have use for in other fields later on when playing around with malicious code or cracking some software.
"The best place to hide a tree, is in a forest"

scatter
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 366
Joined: 01 Jan 2014, 05:22
10

Re: Bitwise operators

Post by scatter »

thx alot cats , yup asm scared the shit out of me too many things to learn and I was like wtf how will I handle this? It would take me years to do it but the problem is am not confident about what I chose as a way to learn asm , here is what I decided to choose from all docs and tutos I gathered and hope if you can tell me if it's enough or not

From : http://opensecuritytraining.info/Training.html" onclick="window.open(this.href);return false;

I chose
IntroX86 ,

•Introduction to Software Exploits (Exploits 1)


•Exploits 2: Exploitation in the Windows Environment


•Intermediate Intel x86: Architecture, Assembly, Applications, & Alliteration



•Advanced x86: Virtualization with Intel VT-x


•Introduction to Reverse Engineering Software


•Reverse Engineering Malware


Assembly primer for hackers == > " onclick="window.open(this.href);return false; all the videos in same series


okay here we come to sthg which is two types of asm that confuses me , there r two asm languages right? one for win and one of AT&T so r both needed? and do you think the docs I posted would be enough? :) thx so much for the effort u r doing with guiding me in this :)

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Bitwise operators

Post by ayu »

There are many types of Assembly languages unfortunately.
But the most popular share a lot of basics which you can learn.
I don't recommend to attempt to learn a complete assembly language unless you have decided to become a hardcore asm programmer.
Find some tutorial on Intels x86 asm and do some basic stuff there to get started.

The docs you found seems good for now :)
I would put them in a priority list though like this (first being top prio):

IntroX86
Introduction to Reverse Engineering Software
Intermediate Intel x86: Architecture, Assembly, Applications, & Alliteration
Introduction to Software Exploits (Exploits 1)
Exploits 2: Exploitation in the Windows Environment
Reverse Engineering Malware
Assembly primer for hackers
(Optional: Advanced x86: Virtualization with Intel VT-x)

Don't get too stuck with asm.
Do a few simple tuts to get the hang of it, then move to something that might be more fun than simply hammering asm, like reverse engineering or writing some exploit code.
And then you can continue with the asm of course to get into more advanced stuff : )

One step at a time.
Setup some milestones and get comfortable with the thought that you wont be learning all this during a night ;)
"The best place to hide a tree, is in a forest"

scatter
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 366
Joined: 01 Jan 2014, 05:22
10

Re: Bitwise operators

Post by scatter »

hehe you have no idea how comfortable u made me feel now , being lost and not knowing what to start with is like having a table full of all kind of food and u have no idea with which to start :D and now u saved me from that I will follow ur plan cats thx alot :D (I wonder how many times I said thx in this thread :p but well it will never be enough ^_^ )

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Bitwise operators

Post by ayu »

scatter wrote:hehe you have no idea how comfortable u made me feel now , being lost and not knowing what to start with is like having a table full of all kind of food and u have no idea with which to start :D and now u saved me from that I will follow ur plan cats thx alot :D (I wonder how many times I said thx in this thread :p but well it will never be enough ^_^ )
You are very welcome! :)

What I said is not written in stone and you can change your schedule at any point if you find any better material or something that seems more interested.
There's no point in forcing yourself to learn this stuff, so use your motivation at all times to push forward in the different areas.

Personally I work with exactly what interests me at this very moment and never waste time on stuff I've grown tired of, since I wont be interested enough to remember all of it :P
But yeah that's how I work, but I of course make exceptions for when my daily job gets boring (gotta put food on the table).

And whenever you can, treat yourself to some bubble tea! :P
(Depends on where you live I guess)

Image
"The best place to hide a tree, is in a forest"

scatter
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 366
Joined: 01 Jan 2014, 05:22
10

Re: Bitwise operators

Post by scatter »

hehe u r right and sure I will but guess what ? I am enjoying asm alooooot more than I did with C or php before :D and that started ofc after the guiding u provided , well it was a bit boring when I had to understand x86 architecture, registers and flags but now that I did that part everything is becoming interesting :D

Post Reply