ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Say you're working with an 8-bit char and the value is something like 129:
If you shift it left 8 times you'll do this:
As you can see, it's impossible for it to be anything but 0. The same happens if you shift it right 8 times. The incoming bits are always 0 and since you're shifting as many time as there are bits in the variable then there's no way to keep any 1s in the variable.
No, I am asking about why shouldnt we shift an operand more than the number of bits it has. Some books say that this is the wrong way.
you shouldnt shift a charater more than 7 times, similarly short more than 15, int 31 .
Even we shift the operand more than the number of bits it has we get zero. Then why shouldnt we perform that shift operations?
I hope you got my question.
Any how thanks for your replies.
if anyone knows the answer please reply me
ok this is simple to explain. int or signed int goes from 2^15- 1 for MAX number and -(2^15-1) for negative number if you notice there's a -1 this is because of what is called clockwise aritmethic which this is not the place to explain :
let's say you have some integer: 0|(some number) this is positive(indicated by the first bit(sign bit)) then the number 1|(some number) is negative well OK imagine you have a number:
0|(all ones) and you add one to it - you will get 1|(all zeros) well this is the smallest negative number by default which is -(2^15-1)= -32767 so when you add one to 32767 you get -32767 , overflow, weird ah? OK well this was for signed int, but what happens if you have unsigned, that means that you do not have any positive or negative numbers and now you can have all the bits to represent your number and you have from 0 to 65535(2^31-1), so when you go from positive MAX +1 you will get zero, basically this is what you do when you >>32 you move your bit from the first place to the sign bit or if it is not on the first place it overflows and still gives you a zero (this actually depends on the architecture and the compiler some people may get a different answer depending on how much they shift)
and you were asking why this is so.
when you initialize a variable you basically allocate a space in memory for it, when you initialize unsigned int in your case, you get 2^32 i believe bits if you shift to the left or to the right of those 32 bits you will actually overwrite over some other data, which may be dangerous, since you don;t know what you will write over. this makes sure that you stay in your 32 bits and don;t F UP your OS or PC or whatever, so just use a long for whatever you are doing and don't worry about it. if you have any other questions ask. Oh i forgot those sizes are architecture dependent as well so don't quote me on that but it is the ANSI C standart what i posted here
thanks for your reply.
So you mean to say that bitwise opearators are performed directly on memory locations. If its on cpu registers then i feel that there is no chance of overwriting. Now again same question why is it so?
If its on memory locations your exp answers my question. If its not please clear my doubt.....
all operations in C are on memory locations(or should I say most) I am not sure if there's any exceptions but if you have ever used malloc() with pointers you know that you cannot do anything with them untill you allocate the memory because it usually gives "segmentation fault" error- tells you that there's something wrong with your memory bounderies and will give you wrong output. Well arrays are the same as pointers but their space is allocated at definition of the array. if you have used arrays and pointers you will know that only the first element is addressable in memory (to address the nth element you give it the address of first element+n(size of type of element)) that is like if you want to access 3rd element in an integer array the compiler tells the OS to start from 0th element and go +3*(2 bytes) and access that element. well when you initialize the variable it;s almost like initializing an array except you are given location in memory and also 2or however many bytes depending on the type so if you try to access the second bit in the integer and output the result you will get crap. Don't ask me how to do that cause I can't think from top of my head. but while you run your program you are using space in memory, where exactly that space is I am not sure, but I know that if you define your variables from type REGISTER they will be in the computer register, but here is 2 AM and is hard to think so I hope this answers ask again if you got questions
What ever you said is correct, but when you are performing arithmetic or some operations you read the data from memory into cpu registers then you perform the operation and place the result in the proper location. I think this is the way how a computer works. So based on this can you explain me the question? hey if you are tired then dont need, we can see it on tomorrow. ok good night
Any other replies on this.....
Look at the lines in bold. The fact that the shifting is done in an isolated register and not in memory is what makes me think there's no way that shifting too many bits would touch any adjacent memory locations.